1.有如下程序,执行后输出结果为______14______。
#include “stdio.h”
main ( )
{ int i=5;
switch(i)
{ case 4:i+=1;
case 5:i+=2;
case 6:i+=3;
default:i+=4;
}
printf("%d\n",i);
}
2.有如下程序段,若grade的值为'C',则输出结果是_____ Medium!____ Pass!______
switch(grade)
{ case 'A':printf("Excellent!\n");
case 'B':printf("Fine!\n");break;
case 'C':printf("Medium!\n");
case 'D':printf("Pass!\n");break;
default:printf("Fail!\n");
}
3.有以下程序,执行后输出结果为_______a=3,b=5__________
#include "stdio.h"
main( )
{ int x=0,y=1,a=2,b=3;
switch(x)
{ case 0:
switch(y)
{ case 0: a++;
case 1: b++; break;
}
case 1: a++;b++; break;
case 2: a++;b++;
}
printf("\na=%d,b=%d",a,b);
}
4.将下列的程序段改用switch语句来实现,使它完成相同的功能。
if(x>=0&&x<10) y=x;
else if(x<20) y=x+3;
else if(x<40) y=-0.5*x+10;
else y=2*x-5;
参考答案:
#include "stdio.h"
main()
{ float x1,x2;
float y;
scanf("%f",&x1);
x2=x1; 。