Chapter 4:
Repetition Structures
Starting Out with Programming Logic & Design
by Tony Gaddis
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter Topics
4.1 Introduction to Repetition Structures
4.2 Condition-Controlled Loops: While, Do-
While, and Do-Until
4.3 Count-Controlled Loops and the For
Statement
4.4 Calculating a Running Total
4.5 Sentinels
4.6 Nested Loops
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2
4.1 Introduction to Repetition Structures
A repetition structure causes a statement or set of
statements to execute repeatedly
Allow a programmer to avoid duplicate code
– Duplicate code makes a program large
– Write a long sequence of statements is time
consuming
– If part of the duplicate code has to be corrected or
changed, then the change has to be done many
times
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3
4.2 Condition-Controlled Loops
• While Loop
– While a condition is true, do some task
• Do-While Loop
– Do some task, while condition is true
• Do-Until Loop
– Do some task, while a condition is false (or until
it’s true)
• With all loops, be careful not to create infinite
loops – always provide a way to break out
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4
4.2 Condition-Controlled Loops
The While Loop – pretest loop
While condition
Statement
Statement
End While
Figure 4-1 The logic
of a While loop
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5
4.2 Condition-Controlled Loops
Declare Integer count = 0
While count < 10
Display count
Set count = count + 1
End While
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-6
4.2 Condition-Controlled Loops
Working with Modules and Loops
• To run a program multiple times, modules can
be put within a loop
Figure 4-3 The main module of Program 4-1
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-7
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-9
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-10
4.2 Condition-Controlled Loops
• The Do-While Loop – posttest loop
• It performs an iteration before
testing its condition
• It always performs at least one
iteration
Do
Statement
Statement
Etc…
While condition
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11
4.2 Condition-Controlled Loops
Declare Integer count = 0
Do
Display count
Set count = count + 1
While count < 10
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15
4.2 Condition-Controlled Loops
The Do-Until Loop
• Loop iterates until a condition is true, but not
all languages support this type of loop
Figure 4-8 The logic of a
Do-Until loop
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-16
4.2 Condition-Controlled Loops
Declare Integer count = 0
Do
Display count
Set count = count + 1
Until count == 10
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-17
4.3 Count-Controlled Loops
A count-controlled loop iterates a specific
number of times
• A for loop is best used for this situation
For counterVariable = startingValue to maxValue
statement
statement
End for
• The loop uses a counter variable to store the
number of iterations
• Three actions are performed with the counter
variable: Initialization, Test, and Increment
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-18
4.3 Count-Controlled Loops
• The logic of a
count-controlled
loop
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-19
4.3 Count-Controlled Loops
For loops can also increment by more than one,
count backwards by decrementing, or allow the
user to control the number of interactions
The for loop in action
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-20
4.3 Count-Controlled Loops
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-21
4.3 Count-Controlled Loops
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-22
4.3 Count-Controlled Loops
Initialization
Test
Increment
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-23
4.3 Count-Controlled Loops
• To make our flowcharts match our pseudocode, we will
use a special flowcharting symbol for the For Statement
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-24
4.3 Count-Controlled Loops
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-25
4.3 Count-Controlled Loops
General loop concerns
• Do not forget to initialize the loop control
variable
• Do not forget to modify the loop control
variable
• Many loops are interchangeable, but generally
– Use while loop when loop may not have to process
– Use do while when it must process at least once
– Use for loop with specific number of iterations
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-26
4.4 Calculating a Running Total
A running total is a sum of number that
accumulates with each iteration of a loop
Figure 4-21 Flowchart for Program 4-17
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-27
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-28
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-29
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-30
4.5 Sentinels
A sentinel is a special value that marks the end of
a list of values, used as stop values for loops
How it can be done
– Ask the user at the end of each loop iteration, if
there is another value to process
– Ask the user at the beginning of the loop, how
many times the loop should process
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-31
4.6 Nested Loops
All loops can be nested, that is, a loop inside of a loop
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-32
Exercises
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Bug Collector
• A bug collector collects bugs every day for seven
days. Design a program that keeps a running total of
the number of bugs collected during the seven days.
• The loop should ask for the number of bugs collected
for each day, and when the loop is finished, the
program should display the total number of bugs
collected.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Bug Collector
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Bug Collector
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Tuition Increase
• At one college, the tuition for a full-time
student is $6,000 per semester. It has been
announced that the tuition will increase by 2
percent each year for the next five years.
• Design a program with a loop that displays the
projected semester tuition amount for the next
five years.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Tuition Increase
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Tuition Increase
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Average Rainfall
• Design a program that uses nested loops to collect data and
calculate the average rainfall over a period of years. The
program should first ask for the number of years. The outer loop
will iterate once for each year.
• The inner loop will iterate twelve times, once for each month.
Each iteration of the inner loop will ask the user for the inches
of rainfall for that month.
• After all iterations, the program should display the number of
months, the total inches of rainfall, and the average rainfall per
month for the entire period.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Average Rainfall
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Average Rainfall
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley