自己制作的c语言教程:第五章(编辑修改稿)内容摘要:
if(„a‟) printf(“%d”,‟a‟)。 说明: if后面的表达式类型任意 语句可以是复合语句 if(x) if(x!=0) if(!x) if(x==0) 例 考虑下面程序的输出结果 : include main() { int x,y。 scanf(“%d,%d”,amp。 x,amp。 y)。 if(xy) x=y。 y=x。 else x++。 y++。 printf(“%d,%d\n”,x,y)。 } Compile Error! 13 第五章 选择结构程序设计 if语句嵌套 : 一般形式: if (expr1) if (expr2) statement1 else statement2 else if(expr3) statement3 else statement4 内嵌 if 内嵌 if if (expr1) if (expr2) statement1 else statement2 内嵌 if if (expr1) { if (expr2) statement1。 } else statement3 内嵌 if if (expr1) statement1 else if(expr3) statement3 else statement4 内嵌 if 14 第五章 选择结构程序设计 –if ~ else 配对原则 : 缺省 { }时, else总是和它上面离它最近的未配对的 if配对 if(……) if(……) if(……) else…... else…... else…... 15 第五章 选择结构程序设计 例: if (a==b) if(b==c) printf(“a==b==c”)。 else printf(“a!=b”)。 修改: if (a==b) { if(b==c) printf(“a==b==c”)。 } else printf(“a!=b”)。 实现 if ~ else 正确配对方法:加 { } 16 第五章 选择结构程序设计 include main() { int a=1,b=1,c=3。 if (ab) if (b0) c=0。 else c++。 printf(“%d\n”,c)。 } 盘上键入 5,则输出结果是什么 include main() { int x。 scanf(“%d”,amp。 x)。 if (++x6) printf(“%d\n”,x) else printf(“%d\n”,x)。 } 结果: 3 结果: 6 17 第五章 选择结构程序设计 include main() { int a=4,b=3,c=5,t=0。 if (ab) t=a。 a=b。 b=t。 if (ac) t=a。 a=c。 c=t。 printf(“%d,%d,%d\n”,a,b,c)。 } include main() { int i=1,j=1,k=1。 if ((j++ || k++) amp。 amp。 i++) printf(“%d ,%d, %d\n”,i,j,k)。 } include main() { int x,y=5。 if (x=y!=0) printf(“%d \n”,x)。 else printf(“%d \n”,x+2)。 } 结果: 503。自己制作的c语言教程:第五章(编辑修改稿)
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。