Chapter 4: Repetition and Iteration
Written by Andrei Guevorkian on 2023-10-10. Illustrated by Dengyijia Liu
In our daily lives, we encounter many repetitive tasks, whether it's setting an alarm each night or brushing our teeth every morning. Similarly, in programming, there's a concept that allows us to handle repetitive actions efficiently: loops. Dive into this chapter to discover how loops work in the world of coding.
Table of Contents
Understanding the Concept of Loops
Understanding the Concept of Loops
Loops are a fundamental concept in programming. They allow us to execute a set of instructions multiple times, which is often referred to as "iteration." Instead of writing the same code over and over again, a loop provides a cleaner way to repeat actions.
Imagine playing your favorite video game. There are certain elements in that game that are repetitive, all thanks to loops. Here are a few:
- Gameplay Music: That catchy tune that plays over and over in the background? It's looped to create a consistent ambiance.
- Non-Playable Characters (NPCs): Characters like guards that patrol the same route or vendors in a marketplace who follow a fixed path are repeating their actions through loops.
- Respawning Items: Ever noticed how some collectible items or power-ups reappear in the same spot after a while? That's a loop ensuring you never run out of challenges or rewards.
- Animated Backgrounds: The moving clouds, flying birds, or flowing rivers that bring a game environment to life? These are often looped animations, adding depth without the need for constant new animations.
Using loops, developers can instruct the game to repeat specific actions seamlessly, enhancing the overall gaming experience. This technique is one of the most widely used and fundamental in a programmer's toolkit!
Use Cases of Looping
Loops are incredibly useful in scenarios where:
- A specific action needs to be repeated multiple times.
- An action must be executed until a particular condition is satisfied.
- We have a collection of items and want to perform the same operation on each item.
Using loops not only saves time but also ensures our code remains clean and efficient.
The While Loop
When you start actually programming, you'll notice that you can't just say "Loop 5 times"; it's a bit more complicated than that.
So in this section, let's take a closer look at the core components of what's known as a while loop.
Example 1: Printing "Hello World" 10 times
One of the classic exercises in programming is to make your program say "Hello World". Let's explore how a loop can be used to print this message 10 times.
In order to print the message once, we write the following line of code:
PRINT "Hello World"
It's that simple to display "Hello World" onto the screen.
Now, to display it 10 times, the inefficient way of doing it is as follows:
PRINT "Hello World"
PRINT "Hello World"
PRINT "Hello World"
// ... (repeat 6 more times)
PRINT "Hello World"
Loops provide us with a much quicker way of doing this. Consider the following:
while counter < 10:
PRINT "Hello World"
In this while loop, we're looking at a simple but fundamental control structure in programming. Here’s how its components work together:
While Loop Keyword: The loop begins with the keyword while
, signaling the start of a while loop. This keyword is crucial as it tells the program that what follows is a condition to be checked.
Condition Check: Next we have a condition check: counter < 10
. Before each iteration, the loop evaluates this condition. If the condition is true, the loop continues and executes the code inside its body. If it's false, the loop stops, and moves onto the code that follows the loop body.
Loop Body: The indented block of code following the while statement is the loop's body (PRINT "Hello World"
in this case). This code is executed repeatedly as long as the loop's condition remains true. Indentation is achieved by using a tab at the start of a line, or by inserting four spaces. This creates a visual hierarchy.
Execution Flow:
- In each loop iteration, the program checks if
counter
is less than 10. - If this is true, the program prints "Hello World".
- After executing the loop body, control returns to the top of the loop, and the condition is checked again.
There is a problem however: what is the value of counter
? Should we start counter
at 0, 1, 5, 10? We need to define this explicitly in our program. Typically, in such scenarios, we start counting from 0. So, let’s initialize counter
to 0 right before the loop starts:
counter = 0
while counter < 10:
PRINT "Hello World"
We're almost done! Now we have a variable called counter
, set to 0, and while counter
is less than 10, the program will print "Hello World" onto the screen. Do you see a problem here?
We want to print 10 times. If we run this program, it will print "Hello World" indefinitely. Why?
Because counter
is 0, and 0 is always less than 10. The while loop will keep asking "is counter < 10
True
or False
?" Since "0 is less than 10" is always True
, the "body" of the while loop will always be executed.
For that reason, we have to update the value of counter
after every iteration, like so:
counter = 0
while counter < 10:
PRINT "Hello World"
counter = counter + 1
Now, counter
starts at 0, but it gets incremented by 1 each time the loop body is entered. Here is step by step what happens:
counter
is set to 0.- Empty line get's skipped.
- The
while
loop condition is checked: sincecounter
is currently set to 0, the program checks if "0 < 10". Because this statement isTrue
, the body of the while loop is entered. - "Hello World" is printed onto the screen.
counter
is incremented by 1. Sincecounter
was previously 0, it is now updated to be 1.- The program returns to check the
while
loop condition: sincecounter
is currently set to 1, the program checks if "1 < 10". Because this statement isTrue
, the body of the while loop is entered. - "Hello World" is printed onto the screen again (for the second time).
counter
is incremented by 1 again. Since counter was previously 1, it is now updated to be 2.- The program returns to the
while
loop condition: withcounter
now at 2, it checks if "2 < 10". This isTrue
, so it enters the loop body once more.
...
- At the 9th iteration,
counter
is 8. The condition "8 < 10" isTrue
, so "Hello World" is printed, andcounter
becomes 9. - At the 10th iteration,
counter
is 9. The condition "9 < 10" is stillTrue
, so "Hello World" is printed for the tenth time, andcounter
is incremented to 10. - The program returns to the
while
loop condition: with counter now at 10, it checks "10 < 10". This statement isFalse
, so the loop does not enter its body, and the program execution moves on, marking the end of the loop.
This example is a fundamental illustration of how loops can automate repetitive tasks, like printing a message multiple times.
Note that we could have had
counter
be the same as the "iteration" number, e.g., at the 1st iteration,counter
is 1, at the 2nd iteration,counter
is 2, etc. All we had to do was startcounter
from 1 instead of 0, but it is convention for computers to start counting from 0.
Example 2: Rocket Launch Countdown
Rocket launches always start with that dramatic countdown: "10, 9, ..., 3, 2, 1, Blast off!". Let's break down how a loop might handle this!
Flowchart representation of the rocket launch countdown.
- Starting Point (Start): The journey begins here, at the "launch pad" of our mission.
- Initialization (Set countdown to 10): As we prepare for launch, the loop gets ready by setting the starting point of our countdown at 10.
- Condition (Is countdown > 0?): The loop asks, "Is there still time left on the countdown?". If the countdown is still greater than 0, the loop continues with the countdown. But once it reaches 0, the loop is exited.
- Execution (Announce countdown): With every tick of the countdown, the loop announces the current number, letting everyone know how many seconds remain until launch.
- Update (Subtract 1 from countdown): After each announcement, the loop decreases the
countdown
by 1. This action ensures that we're progressing toward the blast-off. - Exit Point (Launch the rocket): When
countdown
hits 0, the loop's work is done, and it's time for the rocket to soar into the skies!
Here is the pseudocode version of this situation, with comments (non-executable lines) denoted with //
:
// Initialization: Initialize the countdown
countdown = 10
// Condition: Begin the while loop
WHILE countdown > 0:
// Execution: Print the current countdown number
PRINT countdown
// Update: Decrease the countdown number by 1
countdown = countdown - 1
// Exit Point: Once the loop ends, announce the launch
PRINT "Blast off!"
Overview: This code begins by initializing the countdown
variable to 10. In each iteration of the loop, it checks if countdown
is greater than 0; if so, the current countdown number is printed. After each print, countdown
is reduced by 1. This decrementing continues until countdown
reaches 0, at which point the loop terminates. The final action outside the loop is printing "Blast off!"
signaling the end of the countdown and the launch of the rocket.
Nested Loops
In programming, we can place loops inside other loops, a concept known as nesting. This approach is particularly powerful for handling tasks that involve multiple layers or dimensions.
[Illustration of 2 gears, one large one small]
Nested Loop Example: Amusement Park
Imagine this: you're on a weekend trip to an amusement park with an all-inclusive two-day ticket pass. You're super excited and have decided that on each day, you'll ride 5 different roller coasters.
This situation is a great example of nested loops. Think of the two-day weekend as the outer loop. Now, inside each day (each cycle of the outer loop), there's another loop for the five times you ride the roller coasters.
Here's a breakdown:
- Day Loop (Outer Loop): This runs for two days (Saturday and Sunday).
- Roller Coaster Ride Loop (Inner Loop): Each day, you ride five roller coasters.
The beauty of nested loops is in their simplicity. You're not doing something drastically different each time; you're repeating the same enjoyable action (riding a roller coaster) within the larger context of the weekend.
Here is what the rough pseudocode looks like:
REPEAT 2 days:
REPEAT 5 times:
ride_roller_coaster
look_for_another_roller_coaster
rest
This pseudocode suggests a two-level nested loop: an outer loop for the days and an inner loop for the roller coaster rides each day. Here's the step-by-step process to convert it to an actual while loop:
- Introduce Counter Variables:
dayCount
keeps track of the number of days, andrideCount
keeps track of the number of rides each day. - Translate the Outer Loop: The outer
REPEAT 2 days:
becomeswhile (dayCount < 2):
. - Initialize the Inner Loop Counter:
- At the start of each day (each outer loop iteration), initialize rideCount to 0. - This reset is important to ensure that the inner loop always starts counting from 0 each day.
- Translate the Inner Loop: The inner
REPEAT 5 times:
becomeswhile (rideCount < 5):
. - Increment the Counters:
- Within each loop, increment the respective counter (rideCount++ in the inner loop and dayCount++ in the outer loop) at the end of each iteration. This increment is crucial for the loop's condition to eventually become false, thus preventing infinite loops. - The incrementation ensures that the loop progresses: each ride is counted, and each day is accounted for.
Here is the final result:
dayCount = 0
rideCount = 0
while (dayCount < 2)
rideCount = 0
while (rideCount < 5)
ride_roller_coaster
look_for_another_roller_coaster
rideCount = rideCount + 1
rest
dayCount = dayCounter + 1
Activities
Activity #1
Recall the humanoid robot from Chapter 2 Activity #2. With your understanding of loops from this chapter, describe the commands you would provide to ensure the robot walks across the room. Write the corresponding pseudocode.
A humanoid robot walking.
Answer
To make the robot walk across the room, you would use a series of commands for each step, and then loop through those commands until the robot has crossed the room:
- Lift the right foot 5 centimeters.
- Move the right foot 30 centimeters forward.
- Lower the right foot until it touches the ground.
- Transfer the weight to the right foot.
- Lift the left foot 5 centimeters.
- Move the left foot 60 centimeters forward.
- Lower the left foot until it touches the ground.
- Transfer the weight to the left foot.
- Check if wall is reached. If so, exit loop.
- Otherwise, repeat from step 1.
enfOfRoom = False
while not endOfRoom:
lift_right_foot_5cm
move_right_foot_forward_30cm
lower_right_foot
transfer_weight_right
lift_left_foot_5cm
move_left_foot_forward_60cm
lower_left_foot
transfer_weight_left
if wall_detected:
endOfRoom = True
The variable endOfRoom
is initially set to False
, indicating that the robot hasn't reached the end of the room yet. The while not endOfRoom
loop keeps running as long as endOfRoom
remains False
.
Inside the loop, each step of the walking process is represented by an action (like lift_right_foot_5cm
, move_right_foot_forward_30cm
, etc.). These are assumed to execute the specific movements.
After completing the sequence of steps for both feet, the robot checks if it has reached the wall (using wall_detected
). If the robot detects the wall, it sets endOfRoom
to True
, causing the loop to exit. Otherwise, the loop starts over, repeating the walking sequence.
Activity #2
You're working with a new generation humanoid robot that's designed for physical exercises. Unlike previous models, this robot has advanced kinesthetic intelligence, meaning it understands basic exercise commands without needing a breakdown of every movement. For instance, you don't need to instruct it with "bend your arms 90 degrees" for push-ups; you can simply command "Do a push-up", and it will perform the action. However, you cannot ask it to "Do 10 push-ups", as it can only comprehend one push-up at a time.
Instruct your robot to perform a set of 10 push-ups, followed by 10 sit-ups, and then 10 squats. The robot should complete this set of exercises 5 times.
Answer
To instruct our humanoid robot efficiently, we need to think step-by-step and loop-wise. We want the robot to perform a set of exercises and then repeat that whole set multiple times. This scenario is perfect for nested loops.
Let's break it down:
Outer Loop: Represents the entire exercise set we want the robot to repeat. We want the robot to loop through this set 5 times.
REPEAT 5 times:
Inner Loops: Represents each exercise within the set. Since the robot can only comprehend one movement at a time, we'll use a loop for each exercise type:
Push-ups: We want the robot to do a push-up 10 times. Hence, we loop the do_pushup command 10 times.
REPEAT 10 times:
do_pushup
Similarly, we instruct the robot to repeat the do_situp
and do_squat
commands 10 times.
When pieced together in a nested loop structure, the pseudocode becomes:
REPEAT 5 times:
REPEAT 10 times:
do_pushup
REPEAT 10 times:
do_situp
REPEAT 10 times:
do_squat
To translate the given pseudocode using a while loop, we need to introduce "counter" variables for each level of repetition. Here's how it would look:
setCount = 0
while setCount < 5:
repCount = 0
while repCount < 10:
do_pushup()
repCount += 1
repCount = 0
while repCount < 10:
do_situp()
repCount += 1
repCount = 0
while repCount < 10:
do_squat()
repCount += 1
setCount += 1
In this code:
setCount
keeps track of the number of total sets to be completed.repCount
monitors the number of repetitions (or reps) for each exercise within a set.- Each inner while loop uses
repCount
to execute the specified exercise (push-ups, sit-ups, and squats) 10 times. - After completing each exercise,
repCount
is reset to 0 before moving on to the next exercise. - Once all three exercises are completed in a set,
setCount
is incremented to proceed to the next set, repeating the process until five sets are finished.
Questions
Question 1
Write a program where you make 10 identical sandwiches, all consisting of bread, ham and cheese. Prepare each sandwich sequentially using a loop, ensuring that one sandwich is completed before starting the next.
Question 2
You are thinking of a secret number between 1 and 100, and you ask your friend to guess it. If their guess is too high, you respond with "Too high". If their guess is too low, you say "Too low". Your friend continues guessing based on your feedback until they correctly guess the secret number.
Describe the flow of decisions and responses you would go through until the correct number is identified, as if you were instructing someone to create a flowchart for the game.
Question 3
Is the following program different from the Activity 2 solution. Explain your answer.
REPEAT 5 times:
REPEAT 10 times:
do_pushup
do_situp
do_squat