Studyfin Studyfin ✨ Sign up free

Conditionals

Grade 4 · Coding · Free lesson

Hi there! I am Studyfin, your coding buddy. Today, we will learn how to make programs make smart choices!

Have you ever made a plan? For example, if it rains, you play inside. If it is sunny, you play outside. In coding, we call this a conditional. It lets our program make a choice based on a rule.

sitting

We use an 'if-then' rule to write conditionals. The computer checks if a condition is true. If the condition is true, the computer runs a special block of code.

IF it rains...THEN play inside!

What if the condition is false? We can use 'else' to give the computer a backup plan. If it is NOT raining, then we do the else step and play outside!

ELSE (Not raining)...THEN play outside!
✏️ Worked example

Let us design a block-based program to solve an everyday problem. We want an automatic nightlight to turn on only when it is dark.

  1. First, we identify the problem: the light stays on all day and wastes power.
  2. Next, we look at our sensor. It can detect if it is 'dark' or 'light' in the room.
  3. Then, we write our conditional statement: IF the sensor detects dark.
  4. Next, we add the action: THEN turn the nightlight ON.
  5. Finally, we add the backup plan: ELSE turn the nightlight OFF.
stop youth suicide
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons