If-Then-Else: Making Choices
Programs make choices with if-then-else. Learn how conditions work, use bigger-than and smaller-than checks, and code a ride height checker.
Welcome back!
Step 1 of 12Learn
If it rains...
Every morning you make a choice: if it is raining, then take an umbrella, else leave it at home.
Programs make choices the same way, using conditionals. A condition is a question with a yes-or-no answer, like "Is it raining?" In code, yes is called true and no is called false.
If the condition is true, the program runs one set of steps. If it is false, it runs the else steps instead.
Step 2 of 12Practice · Match the pairs
Match each condition to what the program should do.
Tap an answer, then tap the place where it belongs (or drag it there).
- If the player touches a coin
- If the timer reaches 0
- If the password is right
- If the battery is low
Step 3 of 12Learn
Comparing numbers
Lots of conditions compare numbers. Here are the symbols:
>greater than:10 > 3is true<less than:10 < 3is false>=greater than or equal to:5 >= 5is true===is exactly equal to:4 === 4is true
In JavaScript an if-else looks like this:
if (score > 100) {
return "You win!";
} else {
return "Keep going!";
}Step 4 of 12Flashcards
Comparison symbols
Tap a card to flip it.
Step 5 of 12Practice · Sort into groups
Is each condition true or false?
Tap an item, then tap its group (or drag it there).
Step 6 of 12Practice · Choose one
score is 80. What does this code return? if (score > 100) { return "You win!" } else { return "Keep going!" }
Step 7 of 12Practice · Code challenge
A ride needs riders to be at least 120 cm tall. Finish canRide(height) so it returns "yes" if height is 120 or more, and "no" otherwise.
canRide(130)canRide(120)canRide(110)
Step 8 of 12Note
Step 9 of 12Tap to reveal
If-thens in a game
Jump key pressed?
true: the player jumps. false: the player stays down.
Touching lava?
true: lose a life. false: keep playing.
Lives equal 0?
true: show "Game over." false: the game goes on.
Step 10 of 12Practice · True or false
In an if-else, both the if part and the else part run every time.
Step 11 of 12Practice · Spot the mistake
A game should say "Too cold" when the temperature is below 10. Tap the wrong symbol.
Step 12 of 12Practice · Fill in the blanks
Fill in the blanks
In code, a yes answer is called and a no answer is called .
Lesson complete!