• 6 hours
  • Easy

Free online content available in this course.

course.header.alt.is_certifying

Got it!

Last updated on 1/11/24

Discover Conditional Structures

In life, there are times when we have to make decisions, and then act in accordance with them. The same applies in programming—decisions are made via conditions, to determine whether or not certain blocks of code are launched. 

Let’s go back to our program and imagine a scenario we haven’t seen yet. We want a function which will display a victory or defeat message after the player’s various moves. Specifically, we want the program to display the messageYou won! if the player gets to the end after completing all their moves, or else,You lost!.

Understand If

The keyword  If  is the simplest test structure. It’s used to decide if a particular instruction or block of instructions will be executed or not. If a defined condition is true, then the block of instructions will be executed, but if it’s false, then it won’t. 

For example, we can create an  If  structure to check if the player has reached the end, and if so, display the messageYou won!.

We can do this with the following pseudocode:

If_structure algorithm
Start
   If the player reaches the end :
      display `You won!` 
   End If
End

In this program, if the player reaches the end, the messageYou won! is displayed, otherwise no instruction is launched, so no message is displayed.

To test a condition in an algorithm, we can use comparison operators to compare the parameters of the program.

To carry out these tests, these are the symbols used in most programming languages:

Symbol

Meaning

==

Equal to 

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

!=

Different from

Now let’s imagine we have a variable containing the coordinates of the player in the maze (player_coordinates), and another variable containing the coordinates of the End square ( end_coordinates).

We can replace our pseudocode with this:

If_structure algorithm
Start
   If player_coordinates == end_coordinates :
      display `You won!`
   End If
End

Understand If… Else…

However, we still have a problem—how are we going to display the message to tell the player they’ve lost?

For this, we need a different conditional structure, which can combine the keyword  If  with the keyword  Else . The keyword  If  only tells us that if a condition is true, it will execute a block of instructions, and if the condition is false, it won’t. This means that we need to use the keyword  Else  along with  If  to execute a block of code when the condition is false.

In the case of our maze, we can add a conditional structure at the end of the program to tell us if the player has won or not. If the player is on the End square, it displaysYou won!, otherwise, it displaysYou lost!.

If_structure algorithm
Start
   If player_coordinates == end_coordinates :
      display `You won!`
   Else
      display `You lost!`
   End If
End

Combine Conditions

For the most complex conditions, you can carry out several tests within the same  If . To do this, we’ll need some new keywords:  AND  and  OR :

  • The  AND  operator returns  True  when the conditions to the left and right of it are also  True . When one or both conditions are false,  AND  returns  False .

  • The  OR  operator returns  True  when the condition to the right or left of it, or both, are  True . The only time  OR  returns  False  is when both conditions are false. 

Test AND

Imagine we want to add another condition to the message displayed at the end of the game. This time, once the player has finished their moves, we need to check that they’ve reached the End square and that they’ve made a maximum of 10 moves. 

We could do this:

AND_operator algorithm
Start
   If player_coordinates == end_coordinates AND moves <= 10 :
      display `You won!`
   Else
      display `You lost!`
   End If
End

The condition reads like this: “If the player coordinates and the end coordinates are the same AND the number of moves is fewer than or equal to 10.” 

Test OR

Now let’s look at the problem from another angle. We want to displayYou lost! if the player isn’t on the End square by the time they’ve finished all their moves or if the player has made more than 10 moves.

OR_operator algorithm
Start
   If player_coordinates != end_coordinates OR moves > 10 :
      display `You lost!`
   Else
      display `You won!`
   End If
End

Over to You!

Context

We haven’t yet created the function to move the player to where they want to go. This is exactly what we’re going to do now!

We’re going to use the function  enter()  to ask the player to enter the direction they want to move in, and return what the user enters.

Here are the four possible choices:

  • “Up” moves the player up.

  • “Down” moves the player down.

  • “Right” moves the player right.

  • “Left” moves the player left.

To store the direction of movement entered by the user, you’ll need to put the return value of the  enter()  function in a variable, like this:  direction ← enter().

You’ll also need two variables to store the player’s coordinates,  position_x  and  position_y. These two variables will be passed as a parameter of the function.

Here’s how we can update the player’s coordinates based on their moves:

move up 

add 1 to y 

move down

subtract 1 from y 

move right

add 1 to x 

move left

subtract 1 from x 

We can start the pseudocode off like this:

Moves algorithm (position_x, position_y)
Variable
   direction ← “” : STRING
Start
End

Instructions

You’re going to create a function to manage the player’s moves through the maze. This function should ask the user to enter the direction they want to move in, and change the x and y coordinates according to the direction of the move.

Your Mission

  1. Ask the user to enter the direction they want to move in. For this, assume an  enter()  function will prompt the user to input a direction. The result of  enter()  should be stored in a  direction  variable.

  2. Use a first conditional structure if the user’s input is correct—i.e., it corresponds to one of the four possibilities. Otherwise, display a message stating that the input is incorrect.

  3. If the user’s input is correct, use conditional structures to update the player’s coordinates based on the input.

Check Your Work

Here’s what the result of the exercise should look like:

Moves algorithm (position_x, position_y)
Variable
   direction ← “” : STRING
Start
   direction ← enter()
   If direction == “Up” OR direction == “Down” OR direction == “Right” OR direction == “Left” :
      If direction == “Up” :
      position_y = position_y + 1
      End If
      If direction == “Down” :
      position_y = position_y - 1
      End If
      If direction == “Right” :
      position_x = position_x + 1
      End If
      If direction == “Left” :
      position_x = position_x - 1
      End If
   Else
      display “The input is not correct”
   End If
End

Let’s Recap!

  • Conditional structures control whether certain instructions are executed or not.

  • The  If  instruction is used to execute a certain code block if a condition is true. 

  • The  If-else  instruction is an extended version of the  If  instruction. Using the keyword  else, you can choose which instructions are launched if the condition is not true. 

  • The keywords  AND  and  OR  allow conditions to be combined in conditional structures.

In the next chapter, we’re going to discover a very useful programming concept—loops! What’s more, we can continue our work on conditions in the context of loops. Let’s get started!

Example of certificate of achievement
Example of certificate of achievement