Describe the Repetition | Iteration Constructs (for, while, do-while)
Computers excel at doing the same thing over and over. This is referred to as repetition[1], iteration, or looping. Within a structured program you can use the repetition construct to have the same statements repeatedly executed. Here's a version of the GoForARide program that uses the repetition construct. (Note: We will assume that after 2 hours we've returned home, otherwise we might need to reconsider the last two statements).
Put on cycling clothes
For 2 hours
Ride for 15 minutes
Drink from water bottle
Remove cycling clothes
Take a shower
In this program the statements:
Ride for 15 minutes
Drink from water bottle
are executed repeatedly for 2 hours. This is indicated in the pseudocode by indenting these two statements beneath the repetition statement:
For 2 hours
Here is a flowchart for this program. Note that, like the decision construct, the repetition construct also allows us to modify the straight sequential flow of execution of a program.
Control Flow Constructs - Quiz
In the next lesson we will look at how to break a large program into smaller pieces using subprograms.
Click the Quiz link below to see how well you understand control flow constructs and pseudocode. Control Flow Constructs - Quiz
[1]Repetition: Control flow construct in which a task is performed repeatedly. Also referred to as iteration.