C程序设计ch04-2选择结构程序设计.ppt
第四章第四章 选择结构程序设计选择结构程序设计v4.2 C语句概述语句概述C语句:以“;”作分隔符,编译后产生机器指令.C语句分类v表达式语句:表达式加分号构成。v空语句:;v程序控制语句(9种):if()else switchfor()while()dowhile()continuebreakgotoreturn分支循环辅助控制如 total=total+limit;a=3;func();printf(“Hello,world!n”);v复合语句:用 括起来的一组语句一般形式:数据说明部分;执行语句部分;“”后不加分号语法上和单一语句相同复合语句可嵌套三种基本结构v顺序结构ABAB流程图N-S图PAB真假PBA真假v选择结构A1A2AiAnk=k2k=k1k=knk=ki.l二分支选择结构l多分支选择结构v循环结构l当型循环结构l直到型循环结构PA假真当P为真AAP真假A直到P为真注:A,B,A1.An可以是一个简单语句,也可以是一个基本结构v4.4 分支选择结构分支选择结构简单if语句(p65)v格式:if(expression)statementv执行过程:expressionstatement非0=0例:if(xy)printf(“%d”,x);例 读入a,b,c,d四个整数,输出(a+b)/(c-d)。if(c-d!=0)ratio=(float)(a+b)/(c-d);printf(“Ratio=%fn”,ratio);/*absolute value*/#include int main()int x,y;printf(Enter an integer:);scanf(%d,&x);y=x;if(yabsolute value:%dn,x,y);return 0;例 求一个数的绝对值运行:Enter an integer:-12 integer:-12-absolute value:12expressionstatement1statement2非0=0if else语句 p63v格式:if(expression)statement1 else statement2v执行过程:例 if(c-d!=0)ratio=(float)(a+b)/(c-d);printf(“Ratio=%fn”,ratio);else printf(“c-d is zeron”);例 if(xy)max=x;else max=y;转为表达式:max=(xy)?x:y这里引入了运算符 p40条件运算符?:else if梯状 p66v格式:if(expr1)statement1else if(expr2)statement2else if(expr3)statement3.else statementn expr1statemnt1非0=0expr2expr3statemntnstatemnt3statemnt2非0非0=0=0v执行过程:if(value1000)takeoff=0.25;else if(value800)takeoff=0.2;else if(value600)takeoff=0.15;else if(value400)takeoff=0.1;else takeoff=0;如:if(a=b&x=y)printf(“a=b,x=y”);if(3)printf(“OK”);if(a)printf(“%d”,a);例 考虑下面程序的输出结果:#include int main()int x,y;scanf(“%d,%d”,&x,&y);if(xy)x=y;y=x;else x+;y+;printf(“%d,%dn”,x,y);return 0;Compile Error!if后面的表达式类型任意语句可以是复合语句if(x)if(x!=0)if(!x)if(x=0)/*Be equal or not*/#include int main()int a,b;printf(Enter integer a:);scanf(%d,&a);printf(Enter integer b:);scanf(%d,&b);if(a=b)printf(a=bn);else printf(a!=bn);return 0;例 输入两个数并判断两数相等否运行:Enter integer a:12 Enter integer b:12 a=b 运行:Enter integer a:12 Enter integer b:9 a!=b /*char type*/#include int main()char c;printf(Enter a character:);c=getchar();if(c=0&c=A&c=a&c=z)printf(The character is a lower lettern);else printf(The character is other charactern);return 0;例 判断输入字符种类运行:Enter a character:The character is a control character运行:Enter a character:8 The character is a digit运行:Enter a character:D The character is a capital letter运行:Enter a character:h The character is a lower letter运行:Enter a character:F1 The character is other characterif语句嵌套 p67l一般形式:if(expr1)if(expr2)statement1 else statement2else if(expr3)statement3 else statement4内嵌if内嵌ifif(expr1)if(expr2)statement1 else statement2内嵌ifif(expr1)if(expr2)statement1else statement3 内嵌ifif(expr1)statement1else if(expr3)statement3 else statement4内嵌if例 输入两数并判断其大小关系/*Greater or Less*/#include int main()int x,y;printf(Enter integer x,y:);scanf(%d,%d,&x,&y);if(x!=y)if(xy)printf(XYn);else printf(XYn);else printf(X=Yn);return 0;运行:Enter integer x,y:12,23 XY Enter integer x,y:12,12 X=Yif else 配对原则:缺省 时,else总是和它上面离它最近的未配对的if配对if()if()if()else.else.else.例: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 正确配对方法:加 例 考虑下面程序输出结果:int main()int x=100,a=10,b=20;int v1=5,v2=0;if(a60n”);break;.switch(m)case 5:printf(“Very good!”);case 4:printf(“Good!”);case 3:printf(“Pass!”);case 2:printf(“Fail!”);default :printf(“data error!”);运行结果:m为5时,输出:Very good!Good!Pass!Fail!data error!例 int 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(“na=%d,b=%d”,a,b);return 0;运行结果:a=2,b=1/*Select Label*/#include int main()int c;printf(Enter m or n or h or other:);c=getchar();switch(c)case m:printf(nGood morning!n);break;case n:printf(nGood night!n);break;case h:printf(nHello!n);break;default:printf(n?n);break;return 0;例 根据输入字母输出字符串