Swtich分支结构
swtich 分支结构
一、选择
1、执行下列语句后j等于()
int i=2,j=2;
switch(++i)
{ case 1: ++j;
case 2: ++j;
case 3: ++j
default: ++j;}
A、2
B、3
C、5
D、4
2、下列程序正确的是()
int i=1,j=0;
A、switch(i+j)
B、switch(i+j);
C、switch(i+j)
D、switch(i+j)
{ case 1:i+=j; { case 1:i+=j; { case 1:i+=j; { case i:i+=j;
case 2:i-=j;} case 2:i-=j;} case 1:i-=j;} case 2*i:i-=j;}
3、下列程序段()的功能是输入字符?Y?,回答“Yes”,输入字符?N?,回答“No”
A、switch(getchar())
B、switch(getchar());
{ case …Y?:printf(“Yes\n”); { case …Y?:printf(“Yes\n”);
case …N?:printf(“No\n”);} case …N?:printf(“No\n”);}
C、switch(getchar())
D、switch(getchar());
{ case …Y?:printf(“Yes\n”); { case …Y?:printf(“Yes\n”);
break; break;
case …N?:printf(“No\n”); } case …N?:printf(“No\n”)
4、下述程序段中,正确的是()
A、int x=0,y=10;
B、int x=0,y;
switch(x) switch(x)
{ case y:x++;break; { case x>0:y=1;break;
case y+2:x+=10;break; case x==0:y=0;break;
case y-1:x=-7;break; } case x<0:y=-1;break;}
C、#define y 20
第 1 页共2 页
Swtich分支结构
int x=0,z; D、int x=0,y;
switch(x) switch(x)
{ case 12:z=3;break; { case 3:
case y+1:x+=10;break; case -1:y=2;break;
case y-8:y-=3;break; } case 2: break;}
二、阅读程序题
1、当前整型变量i的值是10,执行下列switch语句后,i的值是_____________
switch(i)
{ case 9: i+=1;
case 10: i+=1;
case 11: i+=1;
default: i+=1;}
2、输出结果____________
main()
{ int x=1,y=0,a=0,b=0;
switch(x)
{ case 1:switch(y)
{ case 0: a++;break;
case 1:b++;break;}
case 2:a++;b++;break;
case 3:a++;b++;}
printf(“a=%d,b=%d”,a,b);}
3、程序结果为_______________
main()
{ int a=15,b=21,m=0;
switch(a%3)
{ case 0: m++; break;
case 1: m++;
switch(b%3)
{ default :m++; break;
case 0:m++;break;}} prinf(“%d”,m);}
第 2 页共2 页
继续阅读