Chapter 5 Repetition and Loop Statements 20061018 chap

  • Slides: 31
Download presentation
Chapter 5 Repetition and Loop Statements 20061018 chap 5

Chapter 5 Repetition and Loop Statements 20061018 chap 5

Repetition in Programs n In most commercial software, you can repeat a process many

Repetition in Programs n In most commercial software, you can repeat a process many times. n n n When using an editor program, you can move the cursor to a program line and perform as many edit operations as you need to. Loop is a control structure that repeats a group of steps in a program. Three C loop control statements n n n 20061018 while for do-while chap 5 2

Flow Diagram of Loop Choice 20061018 chap 5 3

Flow Diagram of Loop Choice 20061018 chap 5 3

Comparison of Loop Kinds 20061018 chap 5 4

Comparison of Loop Kinds 20061018 chap 5 4

The while statement n Counter-controlled loop (or counting loop) n n A loop whose

The while statement n Counter-controlled loop (or counting loop) n n A loop whose required number of iterations can be determined before loop execution begins. The syntax while (loop repetition condition) statement; n n Loop repetition condition: the condition that controls loop repetition. Infinite loop: a loop that executes forever 20061018 chap 5 5

Example count_emp = 0; /* no employees processed yet */ while (count_emp < 7)

Example count_emp = 0; /* no employees processed yet */ while (count_emp < 7) { /* test value of count_emp */ printf("Hours> "); scanf("%d", &hours); printf("Rate> "); scanf("%lf", &rate); pay = hours * rate; printf("Pay is $%6. 2 fn", pay); count_emp = count_emp + 1; /* increment count_emp */ } printf("n. All employees processedn"); 20061018 chap 5 6

Flowchart for a while Loop 20061018 chap 5 7

Flowchart for a while Loop 20061018 chap 5 7

Computing a Sum or a Product in a Loop n n Loops often accumulate

Computing a Sum or a Product in a Loop n n Loops often accumulate a sum or a product by repeating an addition or multiplication operation. Accumulator n 20061018 A variable used to store a value being computed in increments during the execution of a loop. chap 5 8

Example 20061018 chap 5 9

Example 20061018 chap 5 9

Compound Assignment Operators • C provide special assignment operators variable op = expression; 20061018

Compound Assignment Operators • C provide special assignment operators variable op = expression; 20061018 chap 5 10

The for Statement n Three loop control components with the loop body. n n

The for Statement n Three loop control components with the loop body. n n Initialization of the loop control variable, Test of the loop repetition condition, and Change (update) of the loop control variable. The for statement in C supplies a designed place for each of these three components. 20061018 chap 5 11

Counting Loop by for Statement 20061018 chap 5 12

Counting Loop by for Statement 20061018 chap 5 12

for Statement 20061018 chap 5 13

for Statement 20061018 chap 5 13

Increment and Decrement Operators n The increment (i. e. , ++) or decrement (i.

Increment and Decrement Operators n The increment (i. e. , ++) or decrement (i. e. , --) operators are the frequently used operators which take only one operand. for(int i=0; i<100; i++) {…} for(int i=100; i>0; i--) {…} n Side effect: the value of its operand is incremented/decremented by one. 20061018 chap 5 14

Prefix and Postfix Increments n The value of the expression in which the ++/-

Prefix and Postfix Increments n The value of the expression in which the ++/- operator is used depends on the position of the operator. 20061018 chap 5 15

Example: Factorial Function 20061018 chap 5 16

Example: Factorial Function 20061018 chap 5 16

Inc. and Dec. Other Than 1 20061018 chap 5 17

Inc. and Dec. Other Than 1 20061018 chap 5 17

Conditional Loops n n In many programming situations, you will not be able to

Conditional Loops n n In many programming situations, you will not be able to determine the exact number of loop repetitions before loop execution begins. Example: Monitor Gasoline Storage Tank (Fig. 5. 9) 20061018 chap 5 18

20061018 chap 5 19

20061018 chap 5 19

20061018 chap 5 20

20061018 chap 5 20

Loop Design n Problem-Solving Questions for Loop Design n n n n What are

Loop Design n Problem-Solving Questions for Loop Design n n n n What are the inputs? What are the outputs? Is there any repetition? Do I know in advance how many time steps will be repeated? How do I know how long to keep repeating the steps? Sentinel-Controlled Loops Endfile-Controlled Loops Infinite Loops on Faulty Data 20061018 chap 5 21

Sentinel-Controlled Loops n One way to do this is to instruct the user to

Sentinel-Controlled Loops n One way to do this is to instruct the user to enter a unique data value, called a sentinel value, after the last data item. (Fig. 5. 10) 20061018 chap 5 22

Endfile-Controlled Loops n n A data file is always terminated by an endfile character

Endfile-Controlled Loops n n A data file is always terminated by an endfile character that can be detected by the scanf and fscanf functions. EOF stands for the endfile character. 20061018 chap 5 23

Example of Endfile-Controlled Loops 20061018 chap 5 24

Example of Endfile-Controlled Loops 20061018 chap 5 24

Infinite Loops on Faulty Data n 70 7 o n Detect faulty data 20061018

Infinite Loops on Faulty Data n 70 7 o n Detect faulty data 20061018 chap 5 25

Nested Loops n Consist of an outer loop with one or more inner loops.

Nested Loops n Consist of an outer loop with one or more inner loops. (Fig. 5. 13) 20061018 chap 5 26

The do-while Statement n When we know that a loop must execute at least

The do-while Statement n When we know that a loop must execute at least one time. 20061018 chap 5 27

Flag-Controlled Loops n A flag is a variable used to represent whether or not

Flag-Controlled Loops n A flag is a variable used to represent whether or not a certain event has occurred. (Fig. 5. 14) 20061018 chap 5 28

20061018 chap 5 29

20061018 chap 5 29

Summary n Repetition and Loop Statements n n n n Counter-Controller Loop Sentinel-Controller Loop

Summary n Repetition and Loop Statements n n n n Counter-Controller Loop Sentinel-Controller Loop Endfile-Controller Loop Input Validation Loop General Conditional Loop while, for, and do-while Compound Assignment Operators 20061018 chap 5 31