Top Banner
Unit 4 Repetition and Loops
40

Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Dec 30, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Unit 4

Repetition and Loops

Page 2: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

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

Page 3: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.1

Flow Diagram of Loop Choice Process

Page 4: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Types of LoopsTable 5.1

Page 5: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Counter-Controlled Loops

• Used when you know the number of times the loop must execute

• General steps:1. Set loop control variable to 02. While loop control variable < final value• Execute statements• Increase loop control variable by 1

Page 6: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.3

Flowchart for a while Loop

Page 7: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.2

Program Fragment with a Loop

Page 8: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.4

Program to Compute Company Payroll

Page 9: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.

Program to Compute Company Payroll (cont’d)

Page 10: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Compound Assignment Operators

Table 5.3

Page 11: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.5

Using a for Statement in a Counting Loop

Page 12: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

for Loop Style

• Separate lines for each expressionfor (count_emp = 0;

count_emp < number_emp;

count_emp += 1 )

{…}

• All expressions on one linefor (i = 0; i < max; i += 1)

{…}

Page 13: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.6

Comparison of Prefix and Postfix Increments

Page 14: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.7

Function to Compute Factorial

Page 15: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.8 Displaying a Celsius-to-Fahrenheit Conversion Table

Page 16: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

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);}

Page 17: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.9 Program to Monitor Gasoline Storage Tank

Page 18: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.9 Program to Monitor Gasoline Storage Tank (cont’d)

Page 19: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.9

Program to Monitor Gasoline Storage Tank (cont’d)

Page 20: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Sentinel-Controlled Loop

• Data value is used to determine end-point.• General format

1. Get a line of data2. While the sentinel value has not been

encountereda. Process the data line.b. Get another line of data.

Page 21: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Correct vs. Incorrect Sentinel Loop

Correct1. Initialize sum to 02. Get first score3. while score is not the

sentinela. Add score to sumb. Get next score

Incorrect1. Initialize sum to 02. while score is not the

sentinela. Get scoreb. Add score to sum

Page 22: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.10

Sentinel-Controlled while Loop

Page 23: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

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);

Page 24: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

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.

Page 25: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.11 Batch Version of Sum of Exam Scores Program

Page 26: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.12

Program to Process Bald Eagle Sightings for a Year

Page 27: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.12

Program to Process Bald Eagle Sightings for a Year (cont’d)

Page 28: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.13 Nested Counting Loop Program

Page 29: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

do-while Statement

Example 5.9

do {printf(“Enter a letter from A through E>

“);scanf(“%c”, &letter_choice);

} while (letter_choice < ‘A’ || letter_choice > ‘E’);

Page 30: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

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.

Page 31: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.14 Validating Input Using do-while Statement

Page 32: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.15

Structure Chart for Computing Solar Collecting Area Size

Page 33: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.16

Program to Approximate Solar Collecting Area Size

Page 34: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.16 Program to Approximate Solar Collecting Area Size (cont’d)

Page 35: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.16 Program to Approximate Solar Collecting Area Size (cont’d)

Page 36: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Figure 5.16 Program to Approximate Solar Collecting Area Size (cont’d)

Page 37: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Debugger

Debugger allows:– Single-step execution– Setting breakpoints on a statement–Variable inspections

Page 38: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

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);

Page 39: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

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.

Page 40: Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

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.