Hey there! Ready to learn how code makes smart decisions? Let's explore conditionals together!
In coding, we use a design process to solve everyday problems. When we want our program to make a choice, we use a tool called a conditional statement.
A conditional statement is like an 'If-Then' rule. If a condition is true, then the computer runs a specific block of code.
We can also add an 'Else' part. If the condition is false, the computer skips the first choice and does the 'Else' action instead.
✏️ Worked example
Let's design a program to help a smart greenhouse decide when to water a plant. We want to water the plant only when the soil moisture level falls below 40 percent.
Identify the problem: The plant needs water, but only when the soil is dry.
Define the condition: We will measure the soil moisture percentage.
Set the threshold: Our condition is 'Is moisture less than 40?'
Write the If-statement: If moisture < 40, then turn on the water pump.
Write the Else-statement: Else, keep the water pump turned off.
Want the full interactive lesson — quiz, animations, and Finn cheering them on?