What can we do if we want to perform the same action several times?
In the case of our system, we want the user to be able to move through the maze to finish the game, but we don’t know where they’re going to move or the number of moves in advance.
Let me introduce our new friends, loops!
A loop is a structure which repeats the same action several times in a row, exactly like when you put a song on repeat!
We can do the same thing in a computer program. This is what we call iteration. It allows us to write the code once, and execute it several times. In programming, this is known as a loop.
Most programming languages use two types of loop:While
loops andFor
loops.
While
loops are used to execute the body of a loop, until a specific condition becomes false. We mainly use this type of loop when we don’t know how many times the loop will run.We use a
For
loop when we know how many times a loop will run. In other words,For
loops help us to execute a number of steps defined by instructions.
Use While Loops
While
loops are made up of:
A loop condition
A block of code, which is the body of the loop and contains the instructions to be executed at each iteration
The loop condition is evaluated, and if it’s true, the code in the body of the loop is executed. This is repeated until the loop condition becomes false.
In simple terms, we could say that a while loop is a sort of repeated If
instruction, where the code following the If
statement repeats until the If
condition is False.
Here’s the pseudocode that enables players to move through the maze until they reach the End square, using a while loop. We’ll use the move function created in the previous chapter.
While_loop algorithm
Variable
player_position_x ← 0 : INTEGER
player_position_y ← 0 : INTEGER
end_position_x ← 5 : INTEGER
end_position_y ← 5 : INTEGER
Start
While player_position_x != end_position_x AND player_position_y != end_position_y :
moves(player_position_x, player_position_y)
End While
End
Thanks to this loop, you no longer need to write each move the player makes into the program manually.
You’re probably thinking that this is incredibly useful and really easy. And it is—but there’s a catch!
What happens if you create a loop to put a song on repeat?
The song will be played forever and ever, until the end of time! This is because the computer won’t know when to stop, and creates what’s known as an infinite loop. If the loop condition is always true, your program will get stuck in the loop and won’t ever stop!
Use For Loops
In For
loops, we use a loop variable to control the execution of the loop, and the initial value of the variable determines where it starts.
We start by initializing the loop variable with a certain value.
Then, we check if the loop condition is true or not.
If the loop condition is true, the code in the loop body is executed.
Finally, we update the loop variable and move on to the next iteration. This stage is repeated until the loop condition becomes false.
For
loops need three instructions to iterate:
Initialization of the loop variable
Iteration condition
Update of the loop variable
Imagine we want the player to make exactly 10 moves.
We could use the following pseudocode:
For_loop algorithm
Variable
player_position_x ← 0 : INTEGER
player_position_y ← 0 : INTEGER
max_moves ← 10 : INTEGER
Start
For i ← 0 ; i < max_moves; i = i + 1
moves(player_position_x, player_position_y)
End For
End
In this pseudocode, we initialize the loop variable i
at zero, and the loop condition then states that this block of code should iterate for as long as the i
variable is lower than the maximum number of moves. To finish, we update variable i
by adding 1.
We can simplify the line concerning the For
command like this:
For i from 0 to max_moves
Over to You!
Context
At the moment, in our maze, each move the player makes has to be specified in advance in the algorithm, but we’d like to leave it up to the user to choose their moves. We therefore need to use a loop to ask the user what moves they’d like to make.
Instructions
You’re going to create a program to move the player from Start to End, using the move
function we created previously. The player is allowed 15 moves—if they exceed 15, they lose.
Your Mission
Create a loop to move the player, taking into account the player’s position and the number of moves.
Then, create a conditional structure for the loop, to show if the player has won or lost.
Check Your Work
Here’s the result you should get:
Maze algorithm
Variable
player_position_x ← 0 : INTEGER
player_position_y ← 0 : INTEGER
end_position_x ← 5 : INTEGER
end_position_y ← 5 : INTEGER
moves ← 0 : INTEGER
max_moves ← 15 : INTEGER
Start
While player_position_x != end_position_x AND player_position_y != end_position_y AND moves < 15 :
moves(player_position_x, player_position_y)
moves = moves + 1
End While
If player_position_x == end_position_x AND player_position_y == end_position_y :
display “You won!”
Else
display “You lost!”
End If
End
Let’s Recap!
To solve a problem, we can sometimes repeat an instruction several times until a specific condition is fulfilled. This is known as a loop.
There are two main types of loop in programming:
While
loops andFor
loops.While
loops are used to repeat a section of code an indeterminate number of times, until a specific condition is fulfilled.For
loops are used to repeat a section of code a known number of times.
In the first chapter of this part, we saw how to classify data into simple data types. We’re now going to move on to more complex data. Let’s get stuck in!