![[计算机软件及应用]C程序设计ch04 判定、分支和循环.ppt_第1页](http://file.renrendoc.com/FileRoot1/2018-12/23/01dff547-acc3-4c26-95a4-f49f1a61b2eb/01dff547-acc3-4c26-95a4-f49f1a61b2eb1.gif)
![[计算机软件及应用]C程序设计ch04 判定、分支和循环.ppt_第2页](http://file.renrendoc.com/FileRoot1/2018-12/23/01dff547-acc3-4c26-95a4-f49f1a61b2eb/01dff547-acc3-4c26-95a4-f49f1a61b2eb2.gif)
![[计算机软件及应用]C程序设计ch04 判定、分支和循环.ppt_第3页](http://file.renrendoc.com/FileRoot1/2018-12/23/01dff547-acc3-4c26-95a4-f49f1a61b2eb/01dff547-acc3-4c26-95a4-f49f1a61b2eb3.gif)
![[计算机软件及应用]C程序设计ch04 判定、分支和循环.ppt_第4页](http://file.renrendoc.com/FileRoot1/2018-12/23/01dff547-acc3-4c26-95a4-f49f1a61b2eb/01dff547-acc3-4c26-95a4-f49f1a61b2eb4.gif)
![[计算机软件及应用]C程序设计ch04 判定、分支和循环.ppt_第5页](http://file.renrendoc.com/FileRoot1/2018-12/23/01dff547-acc3-4c26-95a4-f49f1a61b2eb/01dff547-acc3-4c26-95a4-f49f1a61b2eb5.gif)
已阅读5页,还剩62页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第四章 判定、分支和循环,4.1 C语句概述 C语句:以“;”作分隔符,编译后产生机器指令. C语句分类 表达式语句:表达式加分号构成。,空语句: ;,程序控制语句(9种):,如 total=total+limit; a=3; func( ); printf(“Hello,world!n”);,复合语句:用 括起来的一组语句 一般形式: 数据说明部分; 执行语句部分; “”后不加分号 语法上和单一语句相同 复合语句可嵌套,三种基本结构 顺序结构,选择结构,二分支选择结构,多分支选择结构,循环结构,当型循环结构,直到型循环结构,注:A,B,A1.An可以是一个简单语句,也可以是一个基本结构,4.2 分支 简单if语句 (p111115) 格式:if (expression) statement 执行过程:,例: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“, ,例 求一个数的绝对值,运行:Enter an integer:-12 integer:-12-absolute value :12,abs(int n) labs(long) fabs(double x) cabs(complex z),if else语句 p115 格式:if (expression) statement1 else statement2 执行过程:,例 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 条件运算符 ?:,条件运算符与表达式 一般形式: expr1 ? expr2 : expr3 执行过程 功能:相当于条件语句,但不能取代一般if语句,例 求 a+|b| printf(“a+|b|=%dn”,b0?a+b:a-b);,例 (a=b)?Y:N (x%2=1)?1:0 (x=0)?x:-x (c=a & c=z)?c-a+A:c,条件运算符可嵌套 如 x0?1:(x0?-1:0) 优先级: 13,结合方向:自右向左 如 ab?a:cd?c:d ab?a:(cd?c:d) expr1、expr2、expr3类型可不同,表达式值取较高的类型,例 x?a:b /x=0,表达式值为b; x0,表达式值为a xy?1:1.5 /xy ,值为1.0; xy ,值为1.5,else if梯状 p122 格式:,if ( expr1 ) statement1 else if (expr2 ) statement2 else if (expr3 ) statement3 . else statementn ,执行过程:,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;,例 百分制成绩转换为五级制 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,例 根据税前收入计算税后收入,2000元起征 0% 不超过500的 5% 超过5002000的部分 10% 超过20005000的部分 15% 超过500020000的部分 20% 超过2000040000的部分 25% 超过4000060000的部分 30% 超过6000080000的部分 35% 超过80000100000的部分 40% 超过100000的部分 45%,Actual=(Income-Base)*对应税率对应速算扣除额,如 Income=8000 500 * 5% 1500 *10% 3000 *15% (8000-7000) *20% =(8000-2000)*20%-5000*20%,如:if(a=b,例 考虑下面程序的输出结果: #include main() int x,y; scanf(“%d,%d”, ,Compile Error!,if后面的表达式类型任意 语句可以是复合语句 if(x) if(x!=0) if(!x) if(x=0),/*Be equal or not*/ #include main() int a,b; printf(“Enter integer a:“); scanf(“%d“, ,例 输入两个数并判断两数相等否,运行: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 ,例 判断输入字符种类,运行: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 character,#include “ctype.h“ iscntrl() isalpha() isdigit() ispunct() isprint() islower() isupper() isalnum(),if语句嵌套 p118 一般形式:,例 输入两数并判断其大小关系,/*Greater or Less*/ #include main() int x,y; printf(“Enter integer x,y:“); scanf(“%d,%d“, ,运行:Enter integer x,y:12,23 XY Enter integer x,y:12,12 X=Y,if else 配对原则:缺省 时,else总是和它上面离它最近的未配对的if配对,例: 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(ab) if(b!=15) if(!v1) x=1; else if(v2) x=10; x=-1; printf(“%d”,x); ,结果:-1,4.3 switch语句(开关分支语句) p125 一般形式: 执行过程:,switch( 表达式) case E1: 语句组 1; break; case E2: 语句组 2; break; . case En: 语句组 n; break; default: 语句组 ; break; ,E1,E2,En是常量表达式,且值必须互不相同 语句标号作用,必须用break跳出 case后可包含多个可执行语句,且不必加 switch可嵌套 多个case可共用一组执行语句, case A: case B: case C: printf(“score60n”); 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!,例 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 Second Division 40 to 49 Third Division 0 to 39 Fail 用Switch Statement.,4.4 循环 概述 C语言可实现循环的语句: 用goto 和 if 构成循环 while 语句 do while 语句 for 语句 goto语句一般格式 p132转移作用,不能用整数作标号 只能出现在goto所在函数内,且唯一 只能加在可执行语句前面 限制使用goto语句,例 用if 和goto语句构成循环,求1+2+100,/*loop using goto statement*/ #include main() int i,sum=0; i=1; loop: if(i=100) sum+=i; i+; goto loop; printf(“%d“,sum); ,sum=0+1 sum=1+2=3 sum=3+3=6 sum=6+4 sum=4950+100=5050,例 从键盘输入一组数据,以0结束输入,求数据和,/*sum of data*/ #include main() int number,sum=0; read_loop: scanf(“%d“, ,例 输入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: A manufacturing company has classified its executives into four levels for the benefit of certain perks. The levels and corresponding perks are shown below: Perks Level - Conveyance Entertainment allowance allowance 1 1000 500 2 750 200 3 500 100 4 250 - An executives gross salary includes basic pay, house rent allowance at 25% of basic pay and other perks. Income tax is withheld from the salary on a percentage basis as follows: Gross salary Tax rate Gross5000 8% Write a program that will 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 一般形式:,while(表达式) 循环体语句;,执行流程:,特点:先判断表达式,后执行循环体 说明: 循环体有可能一次也不执行 循环体可为任意类型语句 下列情况,退出while循环 条件表达式不成立(为零) 循环体内遇break,return,goto 无限循环: while(1) 循环体;,例 用while循环求1+2+100,/*sum of 1 to 100*/ #include main() int i,sum=0; i=1; while(i=100) sum=sum+i; i+; printf(“%d“,sum); ,例 显示110的平方,/*square of every number*/ #include main() int i=1; while(i=10) printf(“%d*%d=%dn“,i,i,i*i); i+; ,运行结果: 1*1=1 2*2=4 3*3=9 4*4=16 5*5=25 6*6=36 7*7=49 8*8=64 9*9=81 10*10=100,dowhile语句 p150 一般形式:,do 循环体语句; while(表达式);,执行流程:,特点:先执行循环体,后判断表达式,至少执行一次循环体 dowhile可转化成while结构,例 用dowhile循环求1+2+100,/*/ #include main() int i,sum=0; i=1; do sum+=i; i+; while(i=100); printf(“%d“,sum); ,例 while和dowhile比较,#include main() int i,sum=0; scanf(“%d“, ,#include main() int i,sum=0; scanf(“%d“, ,for语句 p152 一般形式:,for(expr1 ; expr2 ; expr3) 循环体语句;,执行流程:,for语句一般应用形式:,for(循环变量赋初值;循环条件;循环变量增值) 循环体语句; ,expr1; while(expr2) 循环体语句; expr3; ,例 用for循环求1+2+100,#include main() int i,sum=0; for(i=1;i=100;i+) sum+=i; printf(“%d“,sum); ,for语句中expr1, expr2 ,expr3 类型任意,都可省略,但分号;不可省 无限循环: for(;) for语句可以转换成while结构,“温故”逗号运算符 p154,例 用for循环生成10个随机数并累加,#include #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=10;i+) tmp=rand(); sum+=tmp; printf(“%dnn“,sum); ,#include #include #include main() int i,sum; srand( (unsigned)time(NULL) ); for(sum=0,i=1;i=100;i+) sum+=rand(); printf(“%dnn“,sum); ,各人各次 运行结果不一,逗号运算符和表达式 形式:表达式1,表达式2,表达式n 结合性:从左向右 优先级: 15 逗号表达式的值:等于表达式n的值 用途:常用于循环for语句中,例 a=3*5,a*4 a=3*5,a*4,a+5 例 x=(a=3,6*3) x=a=3,6*a 例 a=1;b=2;c=3; printf(“%d,%d,%d”,a,b,c); printf(“%d,%d,%d”,(a,b,c),b,c);,/a=15,表达式值60,/a=15,表达式值20,/赋值表达式,表达式值18,x=18,/逗号表达式,表达式值18,x=3,/1,2,3,/3,2,3,例:#include main( ) int i; for(i=0;i10;i+) putchar(a+i); ,运行结果:abcdefghij,例:#include main( ) int i=0; for(;i10;i+) putchar(a+i); ,例:#include main( ) int i=0; for(;i10;) putchar(a+(i+); ,例:#include main( ) int i=0; for(;i10;putchar(a+i),i+); ,灵活应用for语句,main() int i,j,k; for(i=0,j=100;i=j;i+,j-) k=i+j; printf(“%d+%d=%dn“,i,j,k); ,#include 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, x1, 算到通项小于0.0001,#include “stdio.h“ int main() float sum=1,x,term; printf(“Input the value of x:“); scanf(“%f“, 这个例子若x的值超过1,则出现列循环。,例 梯形法求数值积分,循环的嵌套 p156 三种循环可互相嵌套,层数不限 外层循环可包含两个以上内循环,但不能相互交叉 嵌套循环的执行流程,(1) while() while() . ,(2) do do while( ); . while( );,(3) while() do while( ); . ,(4) for( ; ;) do while(); while() . ,内循环,内循环,嵌套循环的跳转 禁止: 从外层跳入内层 跳入同层的另一循环 向上跳转,例 循环嵌套,输出九九表 p151,/*Multiplication Table*/ #include main() int i,j; for(i=1;i10;i+) printf(“%4d“,i); printf(“n-n“); for(i=1;i10;i+) for(j=1;j10;j+) printf(j=9)?“%4dn“:“%4d“,i*j); ,for(i=1;i10;i+) for(j=1;j10;j+) printf(j=9)?“%4dn“:“%4d“,i*j);,下面这个例子既是循环语句的应用,也比较充分体现计算机的强大的逻辑运算能力 例 有红、黄、兰、白、紫色的珠子分别放在个包里。 甲说:第二个包是紫色珠子,第四个包是黄色珠子. 乙说:第一个包是红色珠子,第五个包是白色珠子. 丙说:第三个包是白色珠子,第四个包是兰色珠子. 丁说:第二个包是黄色珠子,第五个包是紫色珠子. 每个人都说对一句,说错一句.各个包里的珠子分别是什么颜色的?编制求解程序.,4.5 辅助控制语句 p159165 break语句 p159 功能:在循环语句和switch语句中,终止并跳出循环体或开关体,break只能终止并跳出最近一层的结构 break不能用于循环语句和switch语句之外的任何其它语句之中,例 break举例:输出圆面积,面积大于100时停止,#define PI 3.14159 main() int r; float area; for(r=1;r100) break; printf(“r=%d,area=%.2fn“,r,area); ,例 break举例:小写字母转换成大写字母,直至输入非字母字符,#include main() int i,j; char c; while(1) c=getchar(); if(c=a ,#include “stdio.h“ /*compute 1/(1-x) */ int main() float sum,x,term; int n; printf(“Input the value of x:“); scanf(“%f“, ,break用到前面的例子(为解决列循环)却不方便: 求1/(1-x)=1+x+x2+x3+xn, x1, 算到通项小于0.0001,continue语句 p163 功能:结束本次循环,跳过循环体中一部分尚未执行的语句,进行下一次是否执行循环体的判断 仅用于循环语句中,例 求输入的十个整数中正数的个数及其平均值,/*using continue statement*/ #include main() int i,num=0,a; float sum=0; for(i=0;i10;i+) scanf(“%d“, ,程序举例,分子:1,-1,1,-1 分母:1,3,5,7,.,例 求Fibonacci数列:1,1,2,3,5,8,的前40个数,例 判断m是否素数,例 简单密码系统,例如 Hello,world! 译成密码:Lipps,asvph! 解码:,例pp168 Bonomial Coeffcients are used in the study of bonomial distributions. It is given by B(m,x)=m!/(x!(m-x)!), m=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 gro
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 彩妆培训课堂课件
- 2025江西吉安市遂川县城控人力资源管理有限公司招聘广告技术主管1人备考练习题库及答案解析
- 混凝土工程项目协调管理方案
- 城乡供水一体化总体规划方案
- 微专题9第十七章欧姆定律本章易错题辨析 同步练习 人教版九年级物理全一册(含答案)
- 2025贵州兴仁市残疾人联合会招聘公益性岗位人员考试参考试题及答案解析
- 2025年福建厦门集美人力资源发展有限公司集美分公司招聘1人备考练习试题及答案解析
- 2025云南省楚雄州武定县县级事业单位选调工作人员(26人)考试参考试题及答案解析
- 水库大坝安全性检测方案
- 2025西安安居笙活商业运营管理有限公司招聘(2人)备考练习题库及答案解析
- 家庭适老化改造案例研究及经验分享
- 中邮理财招聘笔试题库2025
- 2024年西师版小学数学二年级上册教案全册
- 2025初中语文新教材培训
- 美术培训学期课件
- 局部晚期头颈部肿瘤治疗讲课件
- 税务会计与税收筹划课件
- 城市照明设计案例赏析
- 环境设计调研方法与策略
- 2025至2030中国气垫船行业发展分析及发展趋势分析与未来投资战略咨询研究报告
- 新媒体视听节目制作
评论
0/150
提交评论