Download presentation
Presentation is loading. Please wait.
1
1 Chapter 3 Flow of Control
2
2 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement The break and continue statements Nested Flow of Control
3
3 The switch Statement multiway conditional statement General form: switch ( switch_exp ) { case constant_exp1: statements; break; // optional... case constant_expn: statements; break; // optional default: statements; break; } //optional
4
4 The switch Statement The effect of a switch: Evaluate the switch_exp. Go to the case label having a constant value that matches the value of the switch_exp. If a match is not found, go to the default label. If there is no default label, terminate the switch. Terminate the switch when a break statement is encountered, or by “falling off the end”.
5
5 The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional case constant_exp2 : statements; case constant_exp3 : statements; …../* no break */ case constant_expi : statements; break; …… case constant_expn: statements; break; // optional …… } Next statement;
6
6 The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional …… case constant_expi : statements; break; …… case constant_expn: statements; break; // optional default: statements; break; } Next statement;
7
7 The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional …… case constant_expi : statements; break; …… case constant_expn: statements; break; // optional } Next statement;
8
8 The switch Statement Example #include int main(void) { int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: break; } return 0; } % gcc switch.c % a.out case 3 case 4 case 5
9
9 The switch Statement Example #include int main(void) { int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); break; case 4: printf("case 4\n"); break; case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: break; } return 0; } % gcc switch.c % a.out case 3
10
10 The switch Statement Example #include int main(void) { int x; x= 3; switch ( x+2 ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); break; case 4: printf("case 4\n"); break; case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: break; } return 0; } % gcc switch.c % a.out case 5
11
11 The switch Statement Rules: The expression in the parentheses following the keyword switch ( switch_exp) must be of integer type. The constant expression following the case labels must be an integer constant unique
12
12 The switch Statement Example The expression in the parentheses following the keyword switch must be of integer type. #include int main(void) { doulbe x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); break; default: break; } return 0; } % gcc switch1.c switch1.c: In function `main': switch1.c:6: error: switch quantity not an integer %
13
13 The switch Statement Example The constant expression following the case labels must be an integer constant. #include int main(void) { int x; x= 3; switch ( x ) { case 1.0: printf("case 1.0\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); break; default: break; } return 0; } % gcc switch2.c switch2.c: In function `main': switch2.c:8: error: case label does not reduce to an integer constant
14
14 The switch Statement Example The constant expression following the case labels must all be unique #include int main(void) { int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 1: printf("Another case 1\n"); break; case 2: printf("case 2\n"); break; default: break; } return 0; } % gcc switch3.c switch3.c: In function `main': switch3.c:9: error: duplicate case value switch3.c:8: error: previously used here
15
15 The switch Statement Summary The switch is a multiway conditional statement. Evaluate the switch_exp. Go to the case label having a constant value that matches the value of the switch_exp. oIf a match is not found, go to the default label. oIf there is no default label, terminate the switch. Terminate the switch when a break statement is encountered, or by “falling off the end”. switch_exp must be of integer type case expressions must be of integer type and must all be unique
16
16 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement The break and continue statements Nested Flow of Control
17
17 The Conditional Operator ?: The general form of a conditional expression: expr1? expr2: expr3 Semantics: First, expr1 is evaluated. If it is nonzero (true), then expr2 is evaluated, and this is the value of the conditional expression as a whole. If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole.
18
18 The Conditional Operator ?: Examples: expr1? expr2: expr3 x=(y<z) ? y : z;
19
19 The Conditional Operator ?: Precedence and Associativity Precedence Just above the assignment operators Associativity: Right to left ……. + -Left to right >=left to right == !=Left to right …..…… ?:Right to left = += -= *= /= etc.Right to left
20
20 The Conditional Operator ?: Examples: int a=1, b=2; double x=7.07; a == b ? a-1 : b+1 ……. + -LRLR >=LRLR == !=LRLR …..…… ?:RLRL = += -= *= /= etc.RLRL ( ) ( ( ) ) a - b < 0 ? x : a + b ( ) ( ) a - b > 0 ? x : a + b ( ) ( ) ( ) ) (
21
21 The Conditional Operator ?: Summary: General form: expr1? expr2: expr3 Semantics: First, expr1 is evaluated. If it is nonzero (true), then expr2 is evaluated, and this is the value of the conditional expression as a whole. If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole. Precedence: Just above the assignment operators Associativity: Right to left
22
22 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement The break and continue statements Nested Flow of Control
23
23 The if and if-else Statement Summary exp is enclosed by parentheses Where appropriate, compound statements should be used to group a series of statements under the control of a single if expression An if or if-else statement can be used as the statement part of another if or if-else statement. an else attaches to the nearest if. If (expr) statement1 else statement2 If (expr) statement1
24
24 The switch Statement Summary The switch is a multiway conditional statement. Evaluate the switch_exp. Go to the case label having a constant value that matches the value of the switch_exp. oIf a match is not found, go to the default label. oIf there is no default label, terminate the switch. Terminate the switch when a break statement is encountered, or by “falling off the end”. switch_exp must be of integer type case expressions must be of integer type and must all be unique
25
25 The Conditional Operator ?: Summary: General form: expr1? expr2: expr3 Semantics: First, expr1 is evaluated. If it is nonzero (true), then expr2 is evaluated, and this is the value of the conditional expression as a whole. If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole. Precedence: Just above the assignment operators Associativity: Right to left
26
26 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement The break and continue statements Nested Flow of Control
27
27 The while Statement General form while (expr) Statement Next statement First expr is evaluated. If expr is nonzero (true), then statement is executed and control is passed back to the beginning of the while loop. Statement is repeatedly until expr is zero (false) Then control passes to next statement.
28
28 The while Statement #include int main(void) { int sum=0, i=1; while(i<=3){ sum=sum+i; i=i+1; } printf(“Sum=%d\n”,sum); return 0; } expression: i<=3 Relational operator <= This expression yields 1(true) or 0(false) statement: sum=sum+i; i=i+1; A group of statements enclosed between { and }.
29
29 The while Statement #include int main(void) { int sum=0, i=1; while(i<=3){ sum=sum+i; i=i+1; } printf(“Sum=%d\n”,sum); return 0; } sum=0 i=1 sum=1 i=2 exp (i<=3): (1<=3)=true Statements are executed sum=sum+i=0+1=1 i=i+1=1+1=2 exp (i<=3): (2<=3)=true Statements are executed sum=sum+i=1+2=3 i=i+1=2+1=3 sum=3 i=3 exp (i<=3): (3<=3)=true Statements are executed sum=sum+i=3+3=6 i=i+1=3+1=4 sum=6 i=4 exp (i<=3): (4<=3)=false while statement is done
30
30 The while statement The while statement First expr is evaluated. If expr is nonzero (true), then statement is executed and control is passed back to the beginning of the while loop. Statement is repeatedly until expr is zero (false) Then control passes to next statement. while (expr) statement; next statement
31
31 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement The break and continue statements Nested Flow of Control
32
32 The for Statement General form Typically: expr1 is used to initialize the loop. expr2 is a logical expression controlling the iteration. expr3 updates the variables used in expr2. for(expr1; expr2; expr3){ statement } next statement
33
33 The for Statement Semantics: First expr1 is evaluated. Then expr2 is evaluated. If expr2 is nonzero (true), othen statement is executed, oexpr3 is evaluated ocontrol passes back to the beginning of the for loop again, except that evaluation of expr1 is skipped. The process continues until expr2 is zero (false), at which point control passes to next statement. for(expr1; expr2; expr3){ statement } next statement
34
34 The for Statement Semantically equivalent to expr1; while(expr2){ statement expr3; } next statement for(expr1; expr2; expr3){ statement } next statement
35
35 The for Statement Example for (expr1; expr2; expr3) statement Next statement #include int main(){ int k=14; int j; for (j=2; j<=k; ++j) { if (k % j == 0) printf("%d is a divisor of %d \n", j, k); } return 0; } What is the output? 2 is a divisor of 14 7 is a divisor of 14 14 is a divisor of 14
36
36 The for Statement Note 1 for (expr1; expr2; expr3) Semicolons are needed Example: for ( i=0, i<n, i+=3) sum +=i; Incorrect for (expr1; expr2; expr3) statement Next statement
37
37 The for Statement Note 2 Any of all of the expressions in a for statement can be missing, but the two semicolons must remain. If expr1 is missing, no initialization step is performed as part of the for loop When expr2 is mission, the rule is that the test is always true. for (expr1; expr2; expr3) statement Next statement
38
38 The for Statement Example of Note 2: expr1 is missing No initialization step is performed as part of the for loop i = 1; sum = 0; for (; i<=10; ++i) sum +=i; for (expr1; expr2; expr3) statement Next statement What the value of sum after the for loop?
39
39 The for Statement Example of Note 2: expr2 is missing The test is always true. i = 1; sum = 0; for (; ; ++i) sum +=i; for (expr1; expr2; expr3) statement Next statement How many iterations? infinity
40
40 The for Statement Note 3 A for statement can be used as the statement part of an if, if-else, while or another for statement for (expr1; expr2; expr3) statement Next statement for (expr1; expr2; expr3) for(……) Next statement for (expr1; expr2; expr3) while(……) if …… else…… Next statement
41
41 The for Statement Example of note 3 for (expr1; expr2; expr3) statement Next statement #include int main(){ int k=14; int j; for (j=2; j<=k; ++j) { if (k % j == 0) printf("%d is a divisor of %d \n", j, k); } return 0; }
42
42 The for Statement — Comma Operator Example Initialize two variables sum, i sum=0; for (i=1; i<=n; ++i) sum+=i; Can we put the initialization of these two variables in one expression?
43
43 The for Statement — Comma Operator The Comma Operator General Form: expr1, expr2 expr1 is evaluated first, then expr2. The comma expression as a whole has the values and type of its right operand.
44
44 The for Statement — Comma Operator The Comma Operator Precedence : lowest Associativity: left to right Example a=0,b=1 oValue of this expression: 1 oType: int
45
45 The for Statement — Comma Operator The Comma Operator Comma operator can be used for multiple initialization and multiple processing of indices in for statement sum=0; for (i=1; i<=n; ++i) sum+=i; for (sum=0, i=1; i<=n; ++i) sum+=i; for (sum=0, i=1; i<=n; sum +=i, ++i) ;
46
46 The for Statement — Comma Operator Example int i, j, k=3; double x = 3.3 i = 1, j = 2, ++ k + 1 k != 7, ++ x * 2.0 + 1 Precedence : lowest Associativity: left to right ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )
47
47 The for Statement Summary The for Statement The Comma Operator: expr1, expr2 oexpr1 is evaluated first, then expr2. oThe comma expression as a whole has the values and type of its right operand. for (expr1; expr2; expr3) statement Next statement
48
48 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement The break and continue statements Nested Flow of Control
49
49 The do Statement General form do statement while (expr); Next statement Typically, expr is a logical expression controlling the iteration.
50
50 The do Statement Semantics First statement is executed, and expr is evaluated. If the value of expr is nonzero (true), then control passes back to the beginning of the do statement, and process repeats itself. When expr is zero (false), then control passes to next statement do statement while (expr); Next statement
51
51 The do Statement Example Suppose we want to read in an integer and want to insist that the integer be positive. do statement While (expr); Next statement do { printf(“Input a positive integer: “); scanf(“%d”, &n); if(error = (n<=0)) printf(“ \n ERROR: Negative value not allowed!\n\n ”); } while (error);
52
52 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement The break and continue statements Nested Flow of Control
53
53 The Break and Continue Statements How to interrupt the normal flow of control? An exit from the innermost enclosing loop or switch statement Causes the current iteration of a loop to stop and the next iteration to begin immediately. while (expr) { statement1; Statement2; } next statement
54
54 The Break and Continue Statements break statement An exit from the innermost enclosing loop (such as a while loop) statement or switch statement
55
55 The Break and Continue Statements break in a switch statement: switch ( switch_exp ) { case constant_exp1: statements; break; // optional case constant_exp2 : statements; break; // optional... case constant_expn: statements; break; // optional default: statements; break; } Next statement; //optional
56
56 The Break and Continue Statements break in a while statement Example: A test for a negative argument is made, and if the test is true, a break statement is used to pass control to the statement immediately following the loop. while(1){ scanf(“%1f”, &x); if(x<0.0) break; printf(“%f\n”, sqrt(x)); } /* break jumps to hear */
57
57 The Break and Continue Statements continue statement Causes the current iteration of a loop to stop and the next iteration to begin immediately. while(exp){ statements; continue; /* maybe in some if-statement*/ /* or switch statement */ statements; /* continue transfers control here to begin next iteration */ }
58
58 The Break and Continue Statements Example Disregard small input value (-0.01 < x < 0.01). while(cnt<n){ scanf(“%1f”, &x); if(-0.01<x && x< +0.01) continue; ++cnt; sum += x; /* continue transfers control here to begin next iteration */ }
59
59 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement The break and continue statements Nested Flow of Control
60
60 Achieve iterative actions The while statement First expr is evaluated. If expr is nonzero (true), then statement is executed and control is passed back to the beginning of the while loop. Statement is repeatedly until expr is zero (false) Then control passes to next statement. while (expr) statement; next statement
61
61 Achieve iterative actions The for Statement First expr1 is evaluated. Then expr2 is evaluated. If expr2 is nonzero (true), othen statement is executed, oexpr3 is evaluated ocontrol passes back to the beginning of the for loop again, except that evaluation of expr1 is skipped. The process continues until expr2 is zero (false), at which point control passes to next statement. for(expr1; expr2; expr3){ statement } next statement
62
62 Achieve iterative actions The do Statement First statement is executed, and expr is evaluated. If the value of expr is nonzero (true), then control passes back to the beginning of the do statement, and process repeats itself. When expr is zero (false), then control passes to next statement do statement while (expr); Next statement
63
63 Achieve iterative actions break statement An exit from the innermost enclosing loop or switch statement continue statement Causes the current iteration of a loop to stop and the next iteration to begin immediately.
64
64 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement Nested Flow of Control
65
65 Nested Flow of Control Flow-of-control statement if, if-else switch for while do
66
66 Nested Flow of Control switch ( switch_exp ) { case constant_exp1: statements; break; // optional case constant_exp2 : statements; break; // optional... case constant_expn: statements; break; // optional default: statements; break; } //optional if (expr) statement1 else statement2 if (expr) statement1
67
67 Nested Flow of Control while (expr) statement; next statement for(expr1; expr2; expr3){ statement } next statement do statement while (expr); Next statement
68
68 Nested Flow of Control Flow-of-control statements can be nested within themselves and within one another. Example if (expr) statement1 else statement2 if (expr) statement1 else statement2 if (expr) statement1 else statement2
69
69 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement and empty statement Select among alternative actions The if and if-else statement The switch statements The conditional Operator Achieve iterative actions The while statement The for statement The do statement Nested Flow of Control
70
70 Chapter 3: Flow of Control End of Chapter 3: Flow of Control
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.