switch case例程

来源:互联网 发布:产品演示动画制作软件 编辑:程序博客网 时间:2024/05/17 01:04

#include "stdafx.h"main(){int x=1,a=0,b=0;switch(x){case 0:b++;case 1:a++;case 2:a++;b++;}printf("a=%d,b=%d/n",a,b);}

运行结果:

a=2,b=1

又如:

#include "stdafx.h"int main(int argc, char* argv[]){ int x,c,m; float y; scanf("%d",&x); if(x<0)  c=-1; else c=x/10; switch(c) { case -1:y=0;break; case 0: y=x;break; case 1:y=10;break; case 2: case 3:y=-0.5*x+20;break; default:y=-2; } if(y!=-2)  printf("y=%g/n",y);//输出%F或%E形式中宽度较小的一种。 else  printf("error/n"); return 0;}

 

原创粉丝点击