版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 5Decision Making and Branching,PROGRAMMING IN ANSI C,9/6/2020,Question,Questions: How do we judge whether a student pass an examination according to his score? How do we decide his grade according to his score? In human nature language: If, then In C: Decision-making statement (branch statem
2、ent),9/6/2020,Chapter 5,In this chapter, we will learn: Decision-making statement: if Conditional operator: ? : Multiway decision-making statement: switch Assisted control statement: break,9/6/2020,3 Forms of if Statement,Form 1 the most simple form if ( test expression ) statement;,if ( score = 60
3、) printf(He passed this examination!);,9/6/2020,3 Forms of if Statement,Form 2 the most general form if ( test expression ) statement 1;elsestatement 2;,if ( score = 60 ) printf(He passed this examination!); else printf(He failed in this examination!);,9/6/2020,3 Forms of if Statement,Form 3 the nes
4、ted form if ( exp1 ) s1;else if ( exp2 ) s2; else elseif ( expn ) sn; else s;,if ( score = 90 ) grade = A; elseif ( score = 80 ) grade = B; elseif ( score = 70 ) grade = C; elseif ( score = 60) grade = D; else grade = E;,9/6/2020,if Statement,The value of the test expression may be any type.If it eq
5、uals zero, it is false, otherwise true.,if ( a=b ,if ( 3 ) printf ( OK );,OK,9/6/2020,if ( a = 2 ) printf ( %d, a ); else printf ( “Wrong );,if ( a = 0 ) printf ( %d, a ); else printf ( “Wrong );,if Statement,The value of the test expression may be any type.If it equals zero, it is false, otherwise
6、true.,2,Wrong,9/6/2020,if Statement,The statement following if or else may be a single statement or a compound statement.,main() int x, y; scanf( %d,%d, ,Compile Error!,Error 6: Misplaced else in function main,9/6/2020,if Statement Program 1,Input 2 real numbers, and output them in ascending order.
7、Step1: Read 2 real numbers into variable x and y. Step2: If x is greater than y, exchange them. Step3: Output x and y.,9/6/2020,if Statement Program 1,main() float x, y, temp; printf (Please input 2 numbers:n); scanf ( %f %f, ,Please input 2 numbers: 13.2 2.1 The 2 numbers are: 2.10, 13.20,9/6/2020,
8、if Statement Program 1,Input 2 real numbers, and output them in ascending order. Step1: Read 2 real numbers into variable x and y. Step2: If x is less than y, output x and y, or else output y and x.,9/6/2020,if Statement Program 1,main() float x, y; printf (Please input 2 numbers:n); scanf ( %f %f,
9、,9/6/2020,if Statement Program 1,Programming Exercises : Input 3 real numbers, and output them in ascending order.(Homework!),9/6/2020,if Statement Program 2,Read in one year, and judge whether it is a leap year or not. Step1: Read in the year. Step2: If the test expression (year%4=0 printf (Please
10、input the year:n); scanf (%d, ,Please input the year: 1900 1900 is not a leap year!,9/6/2020,if Statement Program 2,Step1: Set the flag variable isleap as 0. Step2: Read in the year. Step3: If the test expression (year%4=0 printf (Please input the year:n); scanf (%d, ,Please input the year: 2000 200
11、0 is a leap year!,9/6/2020,if Statement Program 3,Read in one character, and judge which kind of character it is: a figure, or a letter, or other. Step1: Read the character into variable ch. Step2: If (ch=0 printf (Please input the character:n); ch = getchar( ) ; if ( ch = 0 ,Please input the charac
12、ter: M M is a letter!,9/6/2020,Nesting of ifelse Statement,When a series of decision are involved, we may use more than one ifelse statement in nested form.,if (exp1) if (exp2) statement1; else statement2;else if (exp3) statement3; else statement4;,9/6/2020,Nesting of ifelse Statement,“else” matchin
13、g principle: An else is always linked to the closest non-terminated if.,if (exp1) if (exp2) statement1; else statement2;,if (exp1) if (exp2) statement1; elsestatement2;,9/6/2020,if Statement Program 4,According to the score, decide the grade. score = 80: grade A; score = 60 ,9/6/2020,if Statement Pr
14、ogram 4,main() float score; printf ( Please input the score:n ); scanf ( %f, ,Please input the score: 75.5 The grade is B!,9/6/2020,if Statement Program 4,main() float score; char grade; printf ( Please input the score:n ); scanf ( %f, ,9/6/2020,if Statement Program 4,main() float score; char grade;
15、 printf ( Please input the score:n ); scanf ( %f, ,9/6/2020,if Statement Program 4,main() float score;char grade; printf ( Please input the score:n ); scanf ( %f, ,9/6/2020,Conditional Operator ? :,Ternary operator: conditional exp ? exp1 : exp2,9/6/2020,Conditional Operator ? :,Ternary operator: co
16、nditional exp ? exp1 : exp2,if ( a b ) max = a; else max = b;,max = a b ? a : b;,if ( a b ) printf(%d, a); else printf(%d, b);,printf( %d, a b ? a : b);,9/6/2020,Conditional Operator ? :,Ternary operator: conditional exp ? exp1 : exp2 How to output the value of a + |b| ?,if ( b 0 )printf ( %f, a + b
17、 ); else printf ( %f, a b );,printf( %f, b 0 ? a + b : a b );,printf( %f, a + ( b 0 ? b : - b ) );,9/6/2020,Conditional Operator ? :,Ternary operator: conditional exp ? exp1 : exp2 Precedence : higher than assignment operators lower than | Associativity: Right to left Conditional expression can be n
18、ested. x 0 ? 1 : x 0 ? 1 : 0,9/6/2020,Conditional Operator ? :,Ternary operator: conditional exp ? exp1 : exp2 The types of conditional exp and exp1 and exp2 may not be the same. The type of the conditional expression is the same with the highest size type.x = 0 ? a : bx y ? 1 : 1.5,9/6/2020,Conditi
19、onal Operator ? : - Program,Read in a character, if it is an uppercase, output its lowercase equivalent. Otherwise output the read-in character. Step1: Read a character into variable ch. Step2: If ch is an uppercase, convert it into its lowercase equivalent. Step3: Output ch.,9/6/2020,Conditional Op
20、erator ? : - Program,main() char ch; printf ( Please input the charactor:n ); scanf ( %c, ,ch = ( ch = A ,printf ( The charactor is: %cn, ( ch = A ,9/6/2020,switch Statement,One general if statement controls 2 branches. In order to deal with more than 2 branches, we can use the nesting structure of
21、if statement. However, the more branches, the more nesting level, the longer program lines, the more difficultly to read. C supplies a multiway decision statement: switch.,9/6/2020,switch Statement,The general form:,switch (exp) case value1: statement group1; case value2: statement group2; . case va
22、luen: statement groupn; default: statement group; ,9/6/2020,switch Statement,According to the grade, output information. Grade A: output Excellent!. Grade B: output Good!. Grade C: output Pass!. Grade D: output Fail!. Others: output Data error!,switch ( grade ) case A: printf(Excellent!); case B: pr
23、intf(Good!); case C: printf(Pass!); case D: printf(Fail!); default: printf(Data error!); ,if grade=B,Good!Pass!Fail!Data error!,9/6/2020,switch Statement,According to the grade, output information. Grade A: output Excellent!. Grade B: output Good!. Grade C: output Pass!. Grade D: output Fail!. Other
24、s: output Data error!,switch ( grade ) case A: printf(Excellent!); case B: printf(Good!); case C: printf(Pass!); case D: printf(Fail!); default: printf(Data error!); ,switch ( grade ) case A: printf(Excellent!); break; case B: printf(Good!); break; case C: printf(Pass!); break; case D: printf(Fail!)
25、; break; default: printf(Data error!); ,Good!,if grade=B,9/6/2020,switch Statement,The general form:,switch (exp) case value1: statement group1; case value2: statement group2; . case valuen: statement groupn; default: statement group; ,should be integer or character type,If it is not integer or char
26、acter type expression, it will be converted to integer type.,For all that, you should not use real number expression as possible.,switch ( 2.8 ) case 1: printf(1); break; case 2:printf(2); break; case 3:printf(3); break; default:printf(0); ,2,9/6/2020,switch Statement,The general form:,switch (exp)
27、case value1: statement group1; case value2: statement group2; . case valuen: statement groupn; default: statement group; ,should be integer or character type,must be constants or constants expressions, and cant be real number,switch (2) case 1: printf(1);break; case 2:printf(2);break; case 3:printf(
28、3);break; default:printf(0); ,2,switch (2) case 1: printf(1);break; case 1+1:printf(2);break; case 3:printf(3);break; default:printf(0); ,2,int a = 1; switch (2) case 1: printf(1);break; case 1+a:printf(2);break; case 3:printf(3);break; default:printf(0); ,switch (2) case 1: printf(1);break; case 2.
29、0:printf(2);break; case 3:printf(3);break; default:printf(0); ,Error.: Constant expression required in function .,9/6/2020,switch Statement,The general form:,switch (exp) case value1: statement group1; case value2: statement group2; . case valuen: statement groupn; default: statement group; ,should
30、be integer or character type,must be constants or constants expressions, and cant be real number,switch (2) case 1: printf(1);break; case 2:printf(2);break; case 1+1:printf(3);break; default:printf(0); ,Each of these values must be unique within a switch statement.,Error.: Duplicate case in function
31、 .,9/6/2020,switch Statement,The general form:,switch (exp) case value1: statement group1; case value2: statement group2; . case valuen: statement groupn; default: statement group; ,can be zero or more statements, and neednt put braces around these statements.,In if statement, the statement after if
32、 or else must be a single statement or a compound statement.,switch (x+y) case 1: printf(1);printf(n); break; case 2:printf(2); printf(n); break; default:printf(0); printf(n); ,Pay attention to the usage of break.,9/6/2020,switch Statement,The general form:,switch (exp) case value1: statement group1
33、; case value2: statement group2; . case valuen: statement groupn; default: statement group; ,multiple case may share one statement group.,switch (x+y) case 1: case 2: case 3:printf(1); printf(n); break; default:printf(0); ,9/6/2020,switch Statement,The general form:,switch (exp) case value1: stateme
34、nt group1; case value2: statement group2; . case valuen: statement groupn; default: statement group; ,is an optional case, and can be placed anywhere but usually placed at the end.,If present, it will be executed when the expression does not match with any of the case values.,switch ( 0 ) case 1: pr
35、intf(1); break; case 2:printf(2); break; case 3:printf(3); break; default:printf(0); ,0,switch ( 0 ) case 1: printf(1); break; default:printf(0); case 2:printf(2); break; case 3:printf(3); break; ,02,9/6/2020,switch Statement Program 1,According to the score, decide the grade. score = 90: grade A; score = 70 ,Step 1: read in score (float type). Step 2: According to (int) score / 10 , decide and output the grade.,9/6/2020,switch Statement Program 1,main() float score; print
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年九江市中医医院医护人员招聘笔试备考题库及答案详解
- 《核心素养导向:三年级下册“数与代数(一)”结构化复习教案》
- 初中八年级历史《戊戌变法》深度探究教案
- 2026年南通市中医院医护人员招聘笔试备考试题及答案详解
- 2026年上海交通大学医学院附属瑞金医院医护人员招聘笔试备考题库及答案详解
- 2026年教育加盟工程施工协议
- 2026年盐城市第四人民医院医护人员招聘笔试备考试题及答案详解
- 2026年互联网维护采购供应合同
- 2025年四平市铁东区妇幼保健站医护人员招聘考试题库及答案详解
- 2026年广西壮族自治区南溪山医院医护人员招聘考试参考题库及答案详解
- 2026年中考历史模拟试卷及答案(重庆卷)
- 2026年(完整)药物临床试验质量管理规范考试(GCP)题库附答案
- 2026年党建党史党章党纪党史知识竞赛题库及答案
- 2026-2030果蔬产业市场深度调研及发展趋势与投资前景研究报告
- 2026广东东莞市望牛墩镇工程建设中心招聘专业技术人才聘员4人备考题库及答案详解(历年真题)
- 2026年全国高考英语作文热点话题预测
- 房地产-2026年一季度厦门写字楼零售市场报告
- 2025年广东广州市地理生物会考真题试卷(含答案)
- 【《中班幼儿家庭亲子游戏现状与策略研究-以A幼儿园为例》10000字(论文)】
- 国家开放大学《哲学基础》形考任务1-3参考答案
- GB/T 28603-2012无水氟化氢生产技术规范
评论
0/150
提交评论