选择结构
选择结构,又称条件分支是程序的三大基本结构之一,是程序可以处理复杂情况的基本,通过if else 或switch等关键字实现,这里包含大量的选择结构的题目和教程
-
有以下程序:#include <stdio.h> main() { char *s="120119110"; int n0,n1,n2,nn,i; n0=n1=n2=nn=i=0; do { switch(s[i++]) { default:nn++; case '0':n0++; case '1':n1++; case '2':n2++; } }while(s[i]); printf("n0=%d,n1=%d,n2=%d,nn=%d\n",n0,n1,n2,nn); }程序的运行结果是( )。
-
有以下程序#include <stdio.h> main() { int k=5,n=0; do { switch(k) { case 1:case 3:n+=1;k--;break; default:n=0;k--; case 2:case 4:n+=2;k--;break; } printf("%d",n); }while(k>0&&n<5); }程序运行后的输出结果是( )。
-
有以下程序#include <stdio.h> 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++;break; } printf("a=%d,b=%d\n",a,b); }程序的运行结果是( )。
-
有以下程序:#include <stdio.h> main() { int a[]={2,3,5,4},i; for(i=0;i<4;i++) switch(i%2) { case 0: switch(a[i]%2) { case 0:a[i]++;break; case 1:a[i]--; }break; case 1:a[i]=0; } for(i=0;i<4;i++)printf("%d",a[i]); printf("\n"); }程序运行后的输出结果是( )。
-
有以下程序:int i,n; for(i=0; i<8; i++) { n=rand()%5; switch(n) { case 1: case 3:printf("%d\n",n);break; case 2: case 4:printf("%d\n",n);continue; case 0:exit(0); } printf("%d\n",n); }以下关于程序执行情况的叙述中,正确的是( )。
-
有以下程序:#include <stdio.h> main() { int s; scanf("%d",&s); while(s>0) { switch(s) { case 1:printf("%d",s+5); case 2:printf("%d",s+4);break; case 3:printf("%d",s+3); default:printf("%d",s+1);break; } scanf("%d",&s); } }运行时,若输入1 2 3 4 5 0<回车>,则输出结果是( )。
-
有以下程序:#include <stdio.h> main() { int i=1,k=0; for(;i<6;) { switch(i%3) { case 0:k++; case 1:k++;break; case 2:k++;continue; } i+=1; } printf("%d\n",k); }程序的运行情况是( )。
-
有以下程序#include <stdio.h> main() { int a,b; for(a=0; a<3; a++) { scanf("%d", &b); switch(b) { default: printf("%d,", ++b); case 1: printf("%d,", ++b); case 2: printf("%d,", ++b); } } }执行时输入:1 2 3 <回车>,则输出结果是( )。
-
有以下程序:#include <stdio.h> main() { char c; while((c=getchar())!='\n') { switch(c-'2') { case 0: case 1:putchar(c+4); case 2:putchar(c+4);break; case 3:putchar(c+3); default:putchar(c+2);break; } } printf("\n"); }程序运行后从第一列开始输入以下数据:2473<回车>程序的输出结果是( )。
-
有以下程序:#include <stdio.h> main() { int k,n=0;char c,str[]="teach"; for(k=0;str[k];k++) { c=str[k]; switch(k) { case 1: case 3: case 5: putchar(c); printf("%d",++n); break; default:putchar('N'); } } }程序的运行结果是( )。
-
若有定义语句int a,b;double x;则下列选项中没有错误的是( )。
-
若有定义:float x=1.5; int a=1,b=3,c=2;则正确的switch语句是( )。
-
下列叙述中正确的是( )。
-
有以下程序:#include <stdio.h> main() { int a=0,b=0,c=0,d=0; (++a||++b)?++c:++d; printf("%d,%d,%d,%d\n",a,b,c,d);}程序的运行结果是( )。
-
有以下程序:#include <stdio.h> int m1(int x,int y) { return x<=y?x:y; } int m2(int x,int y) { return x<=y?y:x; } int fun(int a,int b){ return a+b; } main() { int x=2,y=3,z=1; printf("%d\n",fun(m1(x,y),m2(y,z))); }程序的运行结果是( )。
-
有以下程序#include <stdio.h> main() { int x; for(x=3;x<6;x++) printf((x%2)?("*%d"):("#%d"),x); printf("\n"); }程序的输出结果是( )。
-
有以下程序:#include <stdio.h> main() { char a='H'; a=(a>='A'&&a<='z')?(a-'A'+'a'):a; printf("%c\n",a); }程序运行后的输出结果是( )。
-
若有定义:int a=0,b=0,c=0,d=0;以下关于C语言表达式:(++aǁ++b)? ++c:++d执行顺序的叙述正确的是 ( )。
-
若有定义:int a=0,b=0,c=0,d=0;有C语言表达式 (a++ && b++)? c++ : d++,以下关于其执行顺序的叙述 正确是( )。
-
有语句:k=x<y?(y<z?1:0):0; 以下选项中,与此语句功能相同的是( )。