Download presentation
Presentation is loading. Please wait.
1
Chapter 5 Decision Making and Branching
PROGRAMMING IN ANSI C
2
11/30/2018 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 statement) 决策、判断
3
Chapter 5 In this chapter, we will learn:
11/30/2018 Chapter 5 In this chapter, we will learn: Decision-making statement: if Conditional operator: ? : Multiway decision-making statement: switch Assisted control statement: break
4
11/30/2018 3 Forms of if Statement Form 1 – the most simple form if ( test expression ) statement; test exp statement true (not 0) false (0) if ( score >= 60 ) printf("He passed this examination!");
5
3 Forms of if Statement Form 2 – the most general form
11/30/2018 3 Forms of if Statement Form 2 – the most general form if ( test expression ) statement 1; else statement 2; test exp statement 1 true(not 0) false(0) statement 2 if ( score >= 60 ) printf("He passed this examination!"); else printf("He failed in this examination!");
6
3 Forms of if Statement Form 3 – the nested form
11/30/2018 3 Forms of if Statement Form 3 – the nested form if ( exp1 ) s1; else if ( exp2 ) s2; else …… else if ( expn ) sn; else s; if ( score >= 90 ) grade = 'A'; else if ( score >= 80 ) grade = 'B'; else if ( score >= 70 ) grade = 'C'; else if ( score >= 60) grade = 'D'; else grade = 'E'; exp1 s1 true false exp2 expn s2 …… sn s
7
11/30/2018 if Statement The value of the test expression may be any type. If it equals zero, it is false, otherwise true. if ( a==b && x==y ) printf ( "a=b, x=y" ); if ( 3 ) printf ( "OK" ); OK
8
11/30/2018 if Statement The value of the test expression may be any type. If it equals zero, it is false, otherwise true. if ( a = 2 ) printf ( "%d", a ); else printf ( “Wrong" ); if ( a = 0 ) printf ( "%d", a ); else printf ( “Wrong" ); Wrong 2
9
11/30/2018 if Statement The statement following if or else may be a single statement or a compound statement. Error … 6: Misplaced else in function main main() { int x, y; scanf( "%d,%d", &x, &y ); if ( x > y ) x=y; y=x; else x++; y++; printf ( "%d,%d\n", x, y); } Compile Error!
10
11/30/2018 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 greater than y, exchange them. Step3: Output x and y. x > y exchange x and y true false output x and y
11
if Statement – Program 1 Please input 2 numbers: 13.2 2.1 main()
11/30/2018 if Statement – Program 1 Please input 2 numbers: The 2 numbers are: 2.10, 13.20 main() { float x, y, temp; printf ("Please input 2 numbers:\n"); scanf ( "%f %f", &x, &y ); if ( x > y ) { temp = x; x = y; y = temp; } printf("The 2 numbers are: %5.2f, %5.2f \n", x, y); }
12
11/30/2018 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. x < y output x and y true false output y and x
13
if Statement – Program 1 main() { float x, y;
11/30/2018 if Statement – Program 1 main() { float x, y; printf ("Please input 2 numbers:\n"); scanf ( "%f %f", &x, &y); printf("The 2 numbers are: "); if ( x < y ) printf ( "%5.2f, %5.2f \n", x, y ); else printf ( "%5.2f, %5.2f \n", y, x ); }
14
11/30/2018 if Statement – Program 1 Programming Exercises : Input 3 real numbers, and output them in ascending order. (Homework!)
15
11/30/2018 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 && year%100!=0)||(year%400==0) is true, then output it is a leap year, or else output it isn’t a leap year.
16
if Statement – Program 2 Please input the year: 1900 main()
11/30/2018 if Statement – Program 2 Please input the year: 1900 1900 is not a leap year! main() { int year; printf ("Please input the year:\n"); scanf ("%d", &year); if ( (year%4==0&&year%100!=0)||(year%400==0) ) printf ( "%d is a leap year!\n", year ); else printf ( "%d is not a leap year!\n", year ); }
17
if Statement – Program 2 Step1: Set the flag variable isleap as 0.
11/30/2018 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 && year%100!=0)||(year%400==0) is true, set the flag variable isleap as 1. Step4: According to the value of isleap, output the year is a leap year or not.
18
if Statement – Program 2 Please input the year: 2000
11/30/2018 if Statement – Program 2 Please input the year: 2000 2000 is a leap year! main() { int year, isLeap = 0; printf ("Please input the year:\n"); scanf ("%d", &year); if ( (year%4==0&&year%100!=0)||(year%400==0) ) isLeap = 1; if ( isLeap ) printf ( "%d is a leap year!\n", year ); else printf ( "%d is not a leap year!\n", year ); }
19
11/30/2018 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' && ch<='9') , it is a figure If (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') , it is a letter Otherwise it is an other character.
20
if Statement – Program 3 Please input the character:
11/30/2018 if Statement – Program 3 Please input the character: M M is a letter! #include <stdio.h> main() { char ch; printf ("Please input the character:\n"); ch = getchar( ) ; if ( ch >= '0' && ch <= '9' ) printf("%c is a figure!", ch); else if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) printf("%c is a letter!", ch); else printf("%c is an other character!",ch); }
21
Nesting of if…else… Statement
11/30/2018 Nesting of if…else… Statement When a series of decision are involved, we may use more than one if…else… statement in nested form. if (exp1) if (exp2) statement1; else statement2; else if (exp3) statement3; else statement4;
22
Nesting of if…else… Statement
11/30/2018 Nesting of if…else… Statement “else” matching principle: An else is always linked to the closest non-terminated if. if (exp1) if (exp2) statement1; else statement2; if (exp1) { if (exp2) statement1; } else statement2;
23
if Statement – Program 4 According to the score, decide the grade.
11/30/2018 if Statement – Program 4 According to the score, decide the grade. score >= 80: grade A; score >= 60 && score < 80: grade B; score < 60 : grade C;
24
if Statement – Program 4 Please input the score: 75.5 The grade is B!
11/30/2018 if Statement – Program 4 Please input the score: 75.5 The grade is B! main() { float score; printf ( "Please input the score:\n" ); scanf ( "%f", &score ); if ( score >= 80 ) printf ( "The grade is: A!\n" ); else if ( score >= 60 ) printf ( "The grade is: B!\n" ); else printf ( "The grade is: C!\n" ); }
25
if Statement – Program 4 main() { float score; char grade;
11/30/2018 if Statement – Program 4 main() { float score; char grade; printf ( "Please input the score:\n" ); scanf ( "%f", &score ); if ( score >= 80) grade = 'A'; else if ( score >= 60 ) grade = 'B'; else grade = 'C'; printf ( "The grade is: %c!\n", grade ); }
26
if Statement – Program 4 main() { float score; char grade;
11/30/2018 if Statement – Program 4 main() { float score; char grade; printf ( "Please input the score:\n" ); scanf ( "%f", &score ); if ( score >= 80) grade = 'A'; if ( score >= 60 && score < 80) grade = 'B'; if ( score < 60) grade = 'C'; printf ( "The grade is: %c!\n", grade ); }
27
if Statement – Program 4 main() { float score; char grade;
11/30/2018 if Statement – Program 4 main() { float score; char grade; printf ( "Please input the score:\n" ); scanf ( "%f", &score ); if ( score > 100 || score < 0 ) printf( "The score is wrong!\n" ); else { if ( score >= 80) grade = 'A'; else if ( score >= 60 ) grade = 'B'; else grade = 'C'; printf ( "The grade is: %c!\n", grade ); }
28
Conditional Operator ? :
11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 conditional exp return the value of exp1 true(not 0) false(0) of exp2
29
Conditional Operator ? :
11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 if ( a > b ) max = a; else max = b; if ( a > b ) printf("%d", a); else printf("%d", b); max = a > b ? a : b; printf( "%d", a > b ? a : b);
30
Conditional Operator ? :
11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 How to output the value of a + |b| ? if ( b > 0 ) printf ( "%f", a + b ); else printf ( "%f", a – b ); printf( "%f", b > 0 ? a + b : a – b ); printf( "%f", a + ( b > 0 ? b : - b ) );
31
Conditional Operator ? :
11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 Precedence : higher than assignment operators lower than || Associativity: Right to left Conditional expression can be nested. x > 0 ? 1 : x < 0 ? –1 : 0
32
Conditional Operator ? :
11/30/2018 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' : 'b' x > y ? 1 : 1.5
33
Conditional Operator ? : - Program
11/30/2018 Conditional 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.
34
Conditional Operator ? : - Program
11/30/2018 Conditional Operator ? : Program main() { char ch; printf ( "Please input the charactor:\n" ); scanf ( "%c", &ch ); if ( ch >= 'A' && ch <= 'Z' ) ch = ch + 32; printf ( "The charactor is: %c\n", ch ); } printf ( "The charactor is: %c\n", ( ch >= 'A' && ch <= 'Z' ) ? ch + 32 : ch ); ch = ( ch >= 'A' && ch <= 'Z' ) ? ch +32 : ch ;
35
11/30/2018 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 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.
36
switch Statement The general form: switch (exp) {
11/30/2018 switch Statement exp statement group1; value1 default case statement group; value2 …… valuen statement group2; statement groupn; The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; }
37
switch Statement Good!Pass!Fail!Data error! if grade=='B'
11/30/2018 switch Statement Good!Pass!Fail!Data error! switch grade “Excellent!” 'A' 'B' 'C' default case “Good!” “Pass!” “Data error” “Fail!” 'D' if grade=='B' 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': printf("Good!"); case 'C': printf("Pass!"); case 'D': printf("Fail!"); default: printf("Data error!"); }
38
switch Statement Good! if grade=='B'
11/30/2018 switch Statement Good! switch grade “Excellent!” 'A' 'B' 'C' default case “Good!” “Pass!” “Data Error” “Fail!” 'D' if grade=='B' 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': 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!"); case 'C': printf("Pass!"); case 'D': printf("Fail!"); default: printf("Data error!"); }
39
should be integer or character type
11/30/2018 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 switch ( 2.8 ) { case 1: printf("1"); break; case 2: printf("2"); case 3: printf("3"); default: printf("0"); } 2 If it is not integer or character type expression, it will be converted to integer type. For all that, you should not use real number expression as possible.
40
should be integer or character type
11/30/2018 switch Statement 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 1+1: printf("2"); break; case 3: printf("3"); break; default: printf("0"); } switch (2) { case 1: printf("1"); break; case 2.0: printf("2"); break; case 3: printf("3"); break; default: printf("0"); } switch (2) { case 1: printf("1"); break; case 2: printf("2"); break; case 3: printf("3"); break; default: printf("0"); } The general form: 2 2 switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } should be integer or character type Error...: Constant expression required in function ... must be constants or constants expressions, and can't be real number
41
should be integer or character type
11/30/2018 switch Statement switch (2) { case 1: printf("1"); break; case 2: printf("2"); break; case 1+1: printf("3"); break; default: printf("0"); } 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 Error...: Duplicate case in function ... must be constants or constants expressions, and can't be real number Each of these values must be unique within a switch statement.
42
11/30/2018 switch Statement In "if" statement, the statement after "if" or "else" must be a single statement or a compound statement. The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } switch (x+y) { case 1: printf("1"); printf("\n"); break; case 2: printf("2"); printf("\n"); default: printf("0"); } can be zero or more statements, and needn't put braces around these statements. Pay attention to the usage of "break".
43
switch Statement switch (x+y) { case 1: case 2: case 3: printf("1");
11/30/2018 switch (x+y) { case 1: case 2: case 3: printf("1"); printf("\n"); break; default: printf("0"); } switch Statement The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } multiple "case" may share one statement group.
44
switch Statement switch ( 0 ) { case 1: printf("1"); break;
11/30/2018 switch Statement switch ( 0 ) { case 1: printf("1"); break; default: printf("0"); case 2: printf("2"); case 3: printf("3"); } switch ( 0 ) { case 1: printf("1"); break; case 2: printf("2"); case 3: printf("3"); default: printf("0"); } 02 The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } If present, it will be executed when the expression does not match with any of the case values. is an optional case, and can be placed anywhere but usually placed at the end.
45
switch Statement – Program 1
11/30/2018 switch Statement – Program 1 According to the score, decide the grade. score >= 90: grade A; score >= 70 && score < 90: grade B; score >= 60 && score < 70: grade C; score < 60 : grade D; Step 1: read in score (float type). Step 2: According to (int) score / 10 , decide and output the grade.
46
switch Statement – Program 1
11/30/2018 switch Statement – Program 1 main() { float score; printf ("Please input the score:\n"); scanf ("%f", &score); switch ( (int) score / 10 ) { case 9: case 10: printf("A\n"); break; case 7: case 8: printf("B\n"); break; case 6: printf("C\n"); break; default: printf("D\n"); }
47
switch Statement – Program 2
11/30/2018 switch Statement – Program 2 It is permitted to nest switch statements. Test the whether the input word is "is" or "it" or "am" or "at". Step1: Declare 2 char variables – c1 and c2. Step2: Read the 2 characters of the input word into c1 and c2. Step3: Judge the word and output the conclusion. If c1=='i', judge c2 If c2=='s', the word is "is"; If c2=='t', the word is "it"; Otherwise, the word isn't any one. If c1=='a', judge c2 If c2=='m', the word is "am"; If c2=='t', the word is "at";
48
switch Statement – Program 2
11/30/2018 main() { char c1, c2; printf ("Please input the word:"); scanf ("%c%c", &c1, &c2); switch ( c1 ) { case 'i': switch(c2) { case 's': printf("is\n"); break; case 't': printf("it\n"); break; default: printf("error\n");} break; case 'a': switch(c2) { case 'm': printf("am\n"); break; case 't': printf("at\n"); break; default: printf("error\n"); } } switch Statement – Program 2
49
switch Statement – Program 2
11/30/2018 main() { char c1, c2; printf ("Please input the word:"); scanf ("%c%c", &c1, &c2); switch ( c1 ) { case 'i': switch(c2) { case 's': printf("is\n"); break; case 't': printf("it\n"); break; default: printf("error\n");} case 'a': switch(c2) { case 'm': printf("am\n"); break; case 't': printf("at\n"); break; default: printf("error\n"); } } switch Statement – Program 2 Execute this program, if the we input "it", what will be outputted? it at error
50
switch Statement – Program 2
11/30/2018 main() { char c1, c2; printf ("Please input the word:"); scanf ("%c%c", &c1, &c2); switch ( c1 ) { case 'i': if (c2=='s') printf("is\n"); else if (c2=='t') printf("it\n"); else printf("error\n "); break; case 'a': if (c2=='m') printf("am\n"); else if (c2=='t') printf("at\n"); default: printf("error\n"); } } switch Statement – Program 2
51
switch Statement – Program 2
11/30/2018 main() { char c1, c2; printf ("Please input the word:"); scanf ("%c%c", &c1, &c2); if ( c1=='i' ) switch(c2) { case 's': printf("is\n"); break; case 't': printf("it\n"); break; default: printf("error\n");} else if ( c1=='a' ) { case 'm': printf("am\n"); break; case 't': printf("at\n"); break; else printf("error\n"); } switch Statement – Program 2
52
11/30/2018 Homework Review Questions P ~5.6, 5.8~5.10 (Write down in your exercise book) Programming Exercises Tip: sqrt()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.