Hi there! I am Studyfin, your coding buddy. Today, we will learn how to make smart decisions in our programs!
Have you ever decided to wear a coat because it was raining? In coding, we call this a conditional. It lets our program make decisions based on real-life events.
We use 'If-Then' rules to tell the computer what to do. If a condition is true, then the computer runs a specific block of code.
What if it is NOT raining? We can use 'Else' to give a backup plan! This keeps our code running smoothly in all situations.
✏️ Worked example
We want to design a program for an automatic nightlight. The light should turn on only when it gets dark in the room. Let us build this step-by-step using our design process.
- First, find the everyday problem: We want to save electricity by keeping the light off during the day.
- Second, name the condition: We need to check if the room is dark.
- Third, write the 'If' block: If the light sensor value is less than 10 (which means it is dark).
- Fourth, write the 'Then' action: Turn the nightlight bulb ON.
- Fifth, write the 'Else' action: Turn the nightlight bulb OFF when it is bright.
- Finally, test your program with a loop so it checks the light level constantly!
Want the full interactive lesson — quiz, animations, and Finn cheering them on?