If it rains, take an umbrella
Change the chance of rain and run a small program that decides whether to take an umbrella. Watch the condition turn true or false, see which branch runs, and fix a condition that gives the wrong answer.
Core concept
An if / else statement checks a condition: when it is true the program runs the then part, otherwise the else part, never both
Topic: Decision making
Programs make decisions all the time. A weather app tells you to take an umbrella only when rain is likely. In a program we write the decision as IF a condition: A question with a yes or no answer that decides what the program does next. All key words THEN do one thing, ELSE do another. The condition is a question with a yes or no answer, such as “is the chance of rain more than 50?”. When the answer is yes (true), the computer runs the THEN part. When it is no (false), it runs the ELSE part. It never runs both, and it always runs one of them.
What you will learn
- Explain what a condition is and when it is true or false.
- Predict which branch of an if / else runs for a given input.
- Find the input where the decision changes (the boundary).
- Fix a condition that gives the wrong decision.
Before you start
- Know the signs > (greater than), < (less than) and = (equal to).
Materials
- A notebook to write your predictions
Equipment
- None
Procedure
- Run the program with an 80% chance of rain and watch which branch lights up.
- Change the chance of rain to 20% and run it again.
- Try exactly 50% and see which branch runs.
- Answer the practice challenges.