C程序设计ch04判定、分支和循环.ppt_第1页
C程序设计ch04判定、分支和循环.ppt_第2页
C程序设计ch04判定、分支和循环.ppt_第3页
C程序设计ch04判定、分支和循环.ppt_第4页
C程序设计ch04判定、分支和循环.ppt_第5页
已阅读5页,还剩64页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第四章 判定、分支和循环 v4.1 C语句概述 C语句:以“;”作分隔符,编译后产生机器指令. C语句分类 v表达式语句:表达式加分号构成。 v空语句: ; v程序控制语句(9种): if( )else switch for( ) while( ) dowhile( ) continue break goto return 分支 循环 辅助控制 如 total=total+limit; a=3; func( ); printf(“Hello,world!n”); v复合语句:用 括起来的一组语句 一般形式: 数据说明部分; 执行语句部分; 说明: v“”后不加分号 v语法上和单一语句相同 v复合语句可嵌套 三种基本结构 v顺序结构 A B A B 流程图N-S图 P AB 真假 P BA 真假 v选择结构 k A1A2AiAn k=k2 k=k1 k=kn k=ki l二分支选择结构 l多分支选择结构 v循环结构 l当型循环结构 l直到型循环结构 P A 假 真 当P为真 A A P 真 假 A 直到P为真 注:A,B,A1.An可以是一个简单语句,也可以是一个基本结构 v4.2 分支 简单if语句 (p111115) v格式:if (expression) statement v执行过程: expr statement 非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 main() int x,y; printf(“Enter an integer:“); scanf(“%d“, y=x; if(yabsolute value:%dn“,x,y); 例 求一个数的绝对值 运行:Enter an integer:-12 integer:-12-absolute value :12 abs(int n) labs(long) fabs(double x) cabs(complex z) expr statement1statement2 非0=0 if else语句 p115 v格式:if (expression) statement1 else statement2 v执行过程: 例 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 这里引入了运算符 p129 条件运算符 ?: 条件运算符与表达式 v一般形式: expr1 ? expr2 : expr3 v执行过程 v功能:相当于条件语句,但不能取代一般if语句 例 if (ab) printf(“%d”,a); else printf(“%d”,b); printf(“%d”,ab?a:b); 例 求 a+|b| printf(“a+|b|=%dn”,b0?a+b:a-b); expr1 取expr2值取expr3值 非0=0 例 (a=b)?Y:N (x%2=1)?1:0 (x=0)?x:-x (c=a x0,表达式值为a xy?1:1.5 /xy ,值为1.0; x1000) takeoff=0.25; else if (value800) takeoff=0.2; else if (value600) takeoff=0.15; else if (value400) takeoff=0.1; else takeoff=0; 例 百分制成绩转换为五级制 p122 80 to 100 Honours 60 to 79 First Division 50 to 59 Second Division 40 to 49 Third Division 0 to 39 Fail 例 累进电费 p124 例 根据收入计算应缴个税 速算扣除额 / 0 25 125 375 1375 3375 6375 10375 15375 2000元起征 0% 不超过500的 5% 超过5002000的部分 10% 超过20005000的部分 15% 超过500020000的部分 20% 超过2000040000的部分 25% 超过4000060000的部分 30% 超过6000080000的部分 35% 超过80000100000的部分 40% 超过100000的部分 45% Tax=(Income-Base)*对应税率对应速算扣除额 如 Income=8000 500 * 5% 1500 *10% 3000 *15% (8000-7000) *20% =(8000-2000)*20%-5000*20% 如:if(a=b if(3) printf(“OK”); if(a) printf(“%d”,a); l说明: uif后面的表达式类型任意 u语句可以是复合语句 u if(x) if(x!=0) if(!x) if(x=0) 例 考虑下面程序的输出结果: #include main() int x,y; scanf(“%d,%d”, if(xy) x=y; y=x; else x+; y+; printf(“%d,%dn”,x,y); Compile Error! /*Be equal or not*/ #include main() int a,b; printf(“Enter integer a:“); scanf(“%d“, printf(“Enter integer b:“); scanf(“%d“, if(a=b) printf(“a=bn“); else printf(“a!=bn“); 例 输入两个数并判断两数相等否 运行:Enter integer a:12 Enter integer b:12 a=b 运行:Enter integer a:12 Enter integer b:9 a!=b /*char type*/ #include main() char c; printf(“Enter a character:“); c=getchar(); if(c=0 printf(“Enter integer x,y:“); scanf(“%d,%d“, if(x!=y) if(xy) printf(“XYn“); else printf(“XY Enter integer x,y:12,12 X=Y if 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 正确配对方法:加 例 考虑下面程序输出结果: main() int x=100,a=10,b=20; int v1=5,v2=0; if(a60n”); break; 例 switch(score) case 5: printf(“Very good!”); case 4: printf(“Good!”); case 3: printf(“Pass!”); case 2: printf(“Fail!”); default : printf(“data error!”); 运行结果:score为5时,输出: Very good! Good! Pass! Fail! data error! 例 void 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); 运行结果:a=2,b=1 /*Select Label*/ #include 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; 例 根据输入字母输出字符串 例 百分制成绩转换为五级制 p122 80 to 100 honours 60 to 79 First Division 50 to 59 First Division 40 to 49 First Division 0 to 39 First Division 用Switch Statement. v4.4 循环 概述 C语言可实现循环的语句: 用goto 和 if 构成循环 while 语句 do while 语句 for 语句 goto语句一般格式 p132 goto 语句标号; . 标号:语句; 标号:语句; . goto 语句标号; v功能:无条件转移语句 v说明: l不能用整数作标号 l只能出现在goto所在函数内,且唯一 l只能加在可执行语句前面 l限制使用goto语句 例 用if 和goto语句构成循环,求1+2+100 /*loop using goto statement*/ #include main() int i,sum=0; i=1; loop: if(i main() int number,sum=0; read_loop: scanf(“%d“, if(!number) goto print_sum; sum+=number; goto read_loop; print_sum: printf(“The total sum is %dn“,sum); 例 输入10个男孩的身高和体重,统计身高超过170体重少 于50公斤的人数。 例pp135 RANGE of Numbers: A survey of the computer macket show that personal computers are sold at varying costs bythe vendors.The following is the list of costs(in hundreds)quoted by some vendors: 35.00, 40.50, 25.00, 31.25, 68.15, 47.00, 26.65, 29.00, 53.45, 62.50 Determine the average cost and the range of values. 例pp136 Pay-Bill Calculations: Read an executives job number, level number, and basic pay and then computer the net salary after withholding income tax. The problem is detailed in the program. while语句 p147 v一般形式: while(表达式) 循环体语句; v执行流程: expr 循环体 假(0) 真(非0) while v特点:先判断表达式,后执行循环体 v说明: l循环体有可能一次也不执行 l循环体可为任意类型语句 l下列情况,退出while循环 u条件表达式不成立(为零) u循环体内遇break,return,goto l无限循环: while(1) 循环体; 例 用while循环求1+2+100 /*sum of 1 to 100*/ #include main() int i,sum=0; i=1; while(i main() int i=1; while(i main() int i,sum=0; i=1; do sum+=i; i+; while(i main() int i,sum=0; scanf(“%d“, do sum+=i; i+; while(i main() int i,sum=0; scanf(“%d“, while(i main() int i,sum=0; for(i=1;i #include #include main() int i, tmp, sum=0; /* Seed the random-number generator with current time so that the numbers will be different every time we run. */ srand( (unsigned)time(NULL) ); for(i=1;i #include #include main() int i,sum; srand( (unsigned)time(NULL) ); for(sum=0,i=1;i main() int x,y=7; float z=4; x=(y=y+6,y/z); printf(“x=%dn“,x); 运行结果:x=3 例:#include main( ) int i; for(i=0;i main( ) int i=0; for(;i main( ) int i=0; for(;i main( ) int i=0; for(;i main() char c; for(;(c=getchar()!=n;) printf(“%c “,c); #include main() int i,c; for(i=0;(c=getchar()!=n;i+=3) printf(“%c “,i+c); 例p161 求1/(1-x)=1+x+x2+x3+xn, x=0.0001;) sum+=term; term*=x; printf(“term=%.5f, Sum=%.5fnn“,term,sum); return 0; 这个例子若x的值超过1,则出现列循环。 例 梯形法求数值积分 0 y x a a+ha+ih a+(i+1)h b f(x) ); 1; 1()*(* )()(*5 . 0 )()()( 2 ) 1()( 2 ) 1()( 2 1 1 1 0 +- main() int i,j; for(i=1;i100) break; printf(“r=%d,area=%.2fn“,r,area); 例 break举例:小写字母转换成大写字母,直至输入非字母字符 #include main() int i,j; char c; while(1) c=getchar(); if(c=a float sum=0; for(i=0;i=x A table of bonomial Coeffcients is required to be printed as follows: Mx 0 1 2 3 4 5 6 7 8 9 10 - 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1 6 1 6 15 20 15 6 1 7 1 7 21 35 35 21 7 1 8 1 8 28 56 70 56 28 8 1 9 1 9 36 84 126 126 84 36 9 1 10 1 10 45 120 210 252 210 120 45 10 1 Problem Analysis: B(m,0) = 1; B(m,x) = B(m,x-1)(m-x+1)/x , x = 1,2,3,.,m B(0,0) =1; 例pp169 In an organization , the employees are grouped according to their basic pay for purpose of certain perks. The pay-range and the number of employees in each group are as follows: GROUP PAY-RANGE NUMBER OF EMPLOYEES 1 750-1500 12 2 1501-3000 23 3 3001-4500 35 4 4501-6000 20 5 above6000 11 Draw a histogram to highlight the group sizes. 例pp171 Minimum Cost: The cost of operation of a unit consists of two components C1 and C2 which can be expressed as functions of a parameter p as follows: C1=30-8p C2=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论