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 andForloops.
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.Â
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
EndThanks 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!
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
EndIn 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
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.
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.
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.
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
EndTo 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 and  For 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!