Coding has entered its decision-making era.
Until now, our instructions have been fairly bossy.
MOVE FORWARD
TURN LEFT
CLAP THREE TIMES
Do this. Then this. Then this. Very efficient.
But what happens when the computer needs to respond to what is happening around it?
What if the robot reaches an obstacle? What if the player catches the treasure? What if the Mika finds the biltong?
Suddenly, following the exact same instructions every time is not enough.
The program needs to make a decision.
That is where conditional statements come in.
A conditional statement tells a program to perform an action only when a particular condition is met. The simplest way to think about it is:
For example:
IF it is raining → THEN take an umbrella
The action only happens because the condition has been met.
No rain? No umbrella.
Unless you are extremely committed to carrying umbrellas.
Conditionals might sound like programming language, but we use this kind of thinking constantly.
The important thing is that there are two parts:
Something we check.
IF the light is red...
What happens when that condition is true.
...THEN stop.
Is the condition true?
If yes → perform the action.
If no → do not perform that action.
And just like that... our code can respond to what is happening.