If-Then Choices
If it is raining, take an umbrella. Learn how algorithms make choices with conditions, if-then-else rules and simple flowcharts.
Welcome back!
Step 1 of 12Learn
Making choices
Every morning you make choices. If it is raining, then you take an umbrella.
Algorithms make choices too. They check a condition: a question with a yes or no answer. "Is it raining?" "Is the score more than 100?" "Is the traffic light red?"
If the answer is yes, the algorithm does one thing. Games are full of these. If you touch a coin, then your score goes up. If you touch a monster, then you lose a life.
Step 2 of 12Practice · True or false
A condition is a question that can be answered with yes or no.
Step 3 of 12Learn
If-then-else
Sometimes you want a plan for both answers. That is an if-then-else:
If it is cold outside, then wear a coat, else wear a T-shirt.
Else means "otherwise." Exactly one of the two choices happens, never both and never neither.
You can draw this as a flowchart. A diamond holds the question. Two arrows come out: one for yes, one for no.
Step 4 of 12Practice · Choose one
A rule says: If the light is green, then walk, else wait. The light is red. What do you do?
Step 5 of 12Practice · Sort into groups
Each card shows a choice. Is it a condition (a yes or no question) or an action (something to do)?
Tap an item, then tap its group (or drag it there).
Step 6 of 12Flashcards
Choice words
Tap a card to flip it.
Step 7 of 12Practice · What would you do?
What would you do?
In a game, the rule is: If you touch a star, then add 5 points, else add 0 points. Sam has 20 points and touches a rock. How many points does Sam have now?
Step 8 of 12Note
Step 9 of 12Practice · Diagram
How a robot vacuum decides
The vacuum hits a chair leg. Tap what it does next.
Tap the shape that answers the question, then press Check.
- Bumped into something?
- Yes
- No
- Turn
Then try a new direction.
- Keep moving forward
Step 10 of 12Practice · Match the pairs
Match each condition to a good action.
Tap an answer, then tap the place where it belongs (or drag it there).
- If your hands are dirty
- If the plant soil is dry
- If it is dark
- If the bus is here
Step 11 of 12Practice · Code challenge
Finish whatToWear(temp). If the temperature is below 50, return "coat", else return "t-shirt". Right now it always returns "t-shirt".
whatToWear(30)whatToWear(75)whatToWear(49)
Step 12 of 12Practice · Fill in the blanks
Fill in the blanks
In an if-then-else, the word means "otherwise."
Lesson complete!