Download presentation
Presentation is loading. Please wait.
Published byMiranda McDonald Modified over 9 years ago
1
Unit 4 Repetition and Loops
2
Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for statement Sentinel-controlled loops Endfile-controlled loops Nested loops do-while statement Flag-controlled loops Debuggers Diagnostic statements Common errors
3
Figure 5.1 Flow Diagram of Loop Choice Process
4
Types of Loops Table 5.1
5
Counter-Controlled Loops Used when you know the number of times the loop must execute General steps: 1.Set loop control variable to 0 2.While loop control variable < final value Execute statements Increase loop control variable by 1
6
Figure 5.3 Flowchart for a while Loop
7
Figure 5.2 Program Fragment with a Loop
8
Figure 5.4 Program to Compute Company Payroll
9
Figure 5. Program to Compute Company Payroll (cont’d)
10
Compound Assignment Operators Table 5.3
11
Figure 5.5 Using a for Statement in a Counting Loop
12
for Loop Style Separate lines for each expression for (count_emp = 0; count_emp < number_emp; count_emp += 1 ) {…} All expressions on one line for (i = 0; i < max; i += 1) {…}
13
Figure 5.6 Comparison of Prefix and Postfix Increments
14
Figure 5.7 Function to Compute Factorial
15
Figure 5.8 Displaying a Celsius- to- Fahrenheit Conversion Table
16
Conditional Loops Execute until a condition is no longer true or becomes true. Example using a while statement: printf("Enter number of values> "); scanf("%d", &num_obs); while (num_obs < 0) { printf("Negative, try again> "); scanf("%d", &num_obs); }
17
Figure 5.9 Program to Monitor Gasoline Storage Tank
18
Figure 5.9 Program to Monitor Gasoline Storage Tank (cont’d)
20
Sentinel-Controlled Loop Data value is used to determine end- point. General format 1.Get a line of data 2.While the sentinel value has not been encountered a.Process the data line. b.Get another line of data.
21
Correct vs. Incorrect Sentinel Loop Correct 1.Initialize sum to 0 2.Get first score 3.while score is not the sentinel a.Add score to sum b.Get next score Incorrect 1.Initialize sum to 0 2.while score is not the sentinel a.Get score b.Add score to sum
22
Figure 5.10 Sentinel-Controlled while Loop
23
Sentinel Loop with a for Statement /* Accumulate sum of all scores. printf(“Enter first score (or %d to quit)> “, SENTINEL); for(scanf(“%d”, &score); score != SENTNEL; scanf(“%d”, &score)) { sum += score; printf(“Enter next score (%d to quit)> ”, SENTINEL);
24
Endfile-Controlled Loops Used to read a file into memory. Loop until EOF. General format: 1.Get the first data value and save input status. 2.While input status does not indicate that end of file has been reached. a.Process data value. b.Get next data value and save input status.
25
Figure 5.11 Batch Version of Sum of Exam Scores Program
26
Figure 5.12 Program to Process Bald Eagle Sightings for a Year
27
Figure 5.12 Program to Process Bald Eagle Sightings for a Year (cont’d)
28
Figure 5.13 Nested Counting Loop Program
29
do-while Statement Example 5.9 do { printf(“Enter a letter from A through E> “); scanf(“%c”, &letter_choice); } while (letter_choice ‘E’);
30
Flag-Controlled Loops Declare a flag variable of type int. Initialize the flag to a value that is not the exit condition. Loop on the flag becoming true or false. Set the flag within the loop to indicate exit condition.
31
Figure 5.14 Validating Input Using do-while Statement
32
Figure 5.15 Structure Chart for Computing Solar Collecting Area Size
33
Figure 5.16 Program to Approximate Solar Collecting Area Size
34
Figure 5.16 Program to Approximate Solar Collecting Area Size (cont’d)
37
Debugger Debugger allows: – Single-step execution – Setting breakpoints on a statement – Variable inspections
38
Diagnostic Calls Use printf to output intermediate results. Define a constant named DEBUG and use a conditional: #define DEBUG 1 if (DEBUG) printf("*** score is %d, sum is %d\n", score, sum);
39
Off by One Errors This loop executes n+1 times: for (count = 0; count <= n; ++count) sum += count; Always test at the loop boundaries to verify the loop does the right thing.
40
Common Errors Forgetting to use curly braces around multiple steps Termination condition never met (infinite loop) Mistyping an equality operator (==) as an assignment operator (=) Confusing do-while and while Mistakes related to operator precedence – Avoid increment, decrement, and compound assignment operators in complex expressions. – Use parentheses to control evaluation order.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.