Download presentation
Presentation is loading. Please wait.
Published byConstance Horn Modified over 9 years ago
1
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall
2
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 2 Outline The for repetition statement switch multiple selection statement break and continue statements Logical operators Structured-programming summary
3
3
4
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 4 The for Repetition Statement (1/3)
5
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 5 The for Repetition Statement (2/3) for loops can usually be rewritten as while loops: initialization; while ( loopContinuationTest ) { statement; increment; } Initialization and increment example: for (int i = 0, j = 0 ; j + i <= 10 ; j++, i++) printf( "%d\n", j + i );
6
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 6 The for Repetition Statement (3/3) Initialization, loop-continuation, and increment can contain arithmetic expressions example : if x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) "Increment" may be negative (decrement) If the loop continuation condition is initially false the body of the for statement is not performed
7
Sum is 2550 Test 7C.E., NCU, Taiwan7 Angela Chih-Wei Tang, 2010
8
fig04_06.c (Part 1 of 2) 8C.E., NCU, Taiwan8 Angela Chih-Wei Tang, 2010
9
Year Amount on deposit 1 1050.00 2 1102.50 3 1157.63 4 1215.51 5 1276.28 6 1340.10 7 1407.10 8 1477.46 9 1551.33 10 1628.89 Test 9
10
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 10 Outline The for repetition statement switch multiple selection statement break and continue statements Logical operators Structured-programming summary
11
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 11 The switch Multiple-Selection Statement Useful when a variable or expression is tested for all the values Format switch ( value ){ case '1': actions case '2': actions default : actions } break ; exits from statement
12
12
13
13
14
14
15
Enter the letter grades. Enter the EOF character to end input. a b c C A d f C E Incorrect letter grade entered. Enter a new grade. D A b ^Z Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1 Test 15
16
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 16 Outline The for repetition statement switch multiple selection statement break and continue statements Logical operators Structured-programming summary
17
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 17 The break and continue Statements – break Causes immediate exit from while for do … while switch Program execution continues with the first statement after the structure Common uses of the break statement escape early from a loop skip the remainder of a switch statement
18
1 2 3 4 Broke out of loop at x == 5 Test 18
19
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 19 The break and continue Statements – continue Skips the remaining statements in the body of while for do … while Proceeds with the next iteration of the loop
20
1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5 Test 20
21
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 21 Outline The for repetition statement switch multiple selection statement break and continue statements Logical operators Structured-programming summary
22
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 22 Logical Operators
23
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 23 Operator Precedence and Associativity
24
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 24 Confusing Equality ( == ) and Assignment ( = ) Operators Dangerous error nonzero : true 0 : false example using == if ( payCode == 4 ) printf( "You get a bonus!\n" ); checks payCode, if it is 4 then a bonus is awarded example using = if ( payCode = 4 ) printf( "You get a bonus!\n" ); sets payCode to 4 4 is nonzero, so expression is true Logic error, not a syntax error
25
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 25 Outline The for repetition statement switch multiple selection statement break and continue statements Logical operators Structured-programming summary
26
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 26 Structured-Programming Summary (1/5) T F if statement (single selection) TF if … else statement (double selection) T F switch statement (multiple selection) break T F T F... Selection
27
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 27 Structured-Programming Summary (2/5) Repetition T F do … while statement T F while statement T F for statement
28
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 28 Structured-Programming Summary (3/5)....... Rule 2 Rule 1 - Begin with the simplest flowchart Rule 2 - Any rectangle can be replaced by two rectangles in sequence
29
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 29 Structured-Programming Summary (4/5) Rule 3 Rule 3 - Replace any rectangle with a control structure
30
C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 30 Structured-Programming Summary (5/5)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.