The 1) for, 2) while, and 3) do-while constructs are all classified as repetition constructs or iteration constructs. These terms are often used interchangeably in computer science to describe control flow statements that allow a block of code to be executed repeatedly.
Here's why they fall under these classifications:
Repetition: They enable the same block of code to be executed multiple times.1
Iteration: Each execution of the loop body is considered an iteration.2 The process of repeatedly executing the code block is called iteration.
Therefore, you can correctly refer to for, while, and do-while loops as either repetition constructs or iteration constructs.
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.
The flowchart in the image illustrates a "cycling routine" using a decision-based repetition construct. Here's a breakdown of the steps:
Flowchart Description:
Start:
"Put on cycling clothes"
Decision Point:
"Ride less than 2 hours?"
This is a conditional check:
If No, the loop goes back to the decision (suggesting to keep riding).
If Yes, the flow continues to the next steps.
Upon "Yes" Condition:
"Ride for 15 minutes"
"Drink from water bottle"
"Remove cycling clothes"
"Take a shower"
Logical Structure:
This flowchart demonstrates a loop based on time:
The rider continues until they have ridden less than 2 hours, which triggers the end-of-ride routine.
The decision ("Ride less than 2 hours?") appears to check whether the condition for ending the ride has been met.
Only when the ride duration is less than 2 hours, the user transitions to cooldown activities.
Key Concepts Highlighted:
Repetition (looping until a time threshold is met)
Conditional logic
Sequential steps for post-exercise activities
Repetition construct represented within a flowchart
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.