Find a course

Module 2: Building Blocks of CodeLesson 4 of 79 min

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.

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.

If-else diagram: the question "Is it raining?" leads to "Take umbrella" if true and "Leave it home" if falseIs it raining?truefalseTake umbrellaLeave it home

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).

  1. If the player touches a coin
  2. If the timer reaches 0
  3. If the password is right
  4. If the battery is low

Step 3 of 12Learn

Comparing numbers

Lots of conditions compare numbers. Here are the symbols:

  • > greater than: 10 > 3 is true
  • < less than: 10 < 3 is false
  • >= greater than or equal to: 5 >= 5 is true
  • === is exactly equal to: 4 === 4 is 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.

Runs in a sandbox in your browser.
  • canRide(130)
  • canRide(120)
  • canRide(110)

Step 8 of 12Note

Step 9 of 12Tap to reveal

If-thens in a game

  1. Jump key pressed?

    true: the player jumps. false: the player stays down.

  2. Touching lava?

    true: lose a life. false: keep playing.

  3. 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 .

Word bank