🔀 Conditions: If… Else…

3rd Grade 💻 Coding

If it’s raining, you take an umbrella; if not, you leave it at home. We make decisions like that all the time — and so do programs. In a game: if the character touches a coin, add a point. In this lesson we learn how to write conditions, and how the computer knows what’s “true” and what’s “false.”

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

The road splits

In a plain sequence, the computer goes through every instruction. A condition changes that: it checks something, and depending on the answer, it chooses which way to go. The basic structure is “if — then — else.” If the condition is met, one group of instructions runs. If it isn’t, a different group runs — or the program simply moves on.

In Python, as in most languages, the word for a condition is if, and the word for the alternative is else. For example: if age >= 12: print("You can sign up") — if the age is greater than or equal to 12, show “You can sign up.” Usually this is written on two lines: the condition on one line and the instruction on the line below it, with a few spaces at the start. In Scratch there’s a block shaped like an open mouth that says “if… then…,” and you put the blocks that should run inside the mouth.

True or false

Every condition is really a question whose answer is “yes” or “no.” In computers these answers are called true and false — True and False — and this kind of information is called Boolean, after the English mathematician George Boole. “Is 7 greater than 5?” — true. “Is 3 equal to 4?” — false. There’s no “maybe” and no “sort of.”

To ask questions like these, we use comparison signs: greater than (>), less than (<), greater than or equal to (>=), and equal to, which in Python is written with two equals signs: ==. Why two? Because a single equals sign is already taken: it’s used to store a value in a variable. x = 5 means “put 5 into x,” while x == 5 means “is x equal to 5?” Mixing them up is a very common beginner mistake.

And, or, not

Sometimes you need to check several things at once. “And” (and) gives true only if both parts are true: “If I have a ticket and I arrived on time, I get into the show.” “Or” (or) gives true if at least one of them is true: “If it’s raining or it’s way too hot, we stay inside for recess.” “Not” (not) flips the answer: “If I have not finished my homework, no screens.”

You can also chain several options together. Python has a special word for this, elif — short for “else if.” For example, for a test score: if the score is 90 or more, “Excellent”; else, if it’s 70 or more, “Good”; else, “Time to review the material.” The computer checks the options in order and stops at the first one that’s true, so it’s important to put them in the right order.

Conditions in games and in life

Almost every game is full of conditions. If the player pressed the right arrow, the character moves right. If the character touches a wall, it stops. If there are no lives left, “Game Over” appears. Apps are full of them too: if the password is correct, you get in; otherwise, a message pops up. A smart traffic light checks whether cars are waiting before it changes color.

When you write a condition, think about every case: what happens if the score is exactly 90? What if the user typed a negative number? Programmers pay special attention to “edge cases” — the values right on the boundary — because that’s where lots of bugs hide. Write out all the possibilities in a little table before you write the code.

Key facts

  • A condition checks a yes-or-no question and, depending on the answer, chooses which instructions to run.
  • In Python and many other languages, you write if for the condition and else for the alternative.
  • The answers to a condition are true or false (True and False), a kind of information called Boolean.
  • In Python, == means “is it equal?” while = stores a value in a variable.
  • and needs both parts to be true, or needs just one, and not flips the answer.

Quick quiz

  1. What does a condition do in a program?

  2. Which word is used for a condition in Python?

  3. What runs when the condition isn’t met and there’s an else?

  4. What’s the answer to the condition “7 is greater than 5”?

  5. In Python, what’s the difference between = and ==?

  6. “I have a ticket and I arrived on time” — when do you get in?

  7. “It’s raining or it’s way too hot” — when do you stay inside?

  8. What is an “edge case”?

💭 Think and talk about it

  1. Write three conditions from your day in the form “if… then… else…”.
  2. In a game where “the game ends when you run out of lives,” exactly what condition does the computer check — and how often, do you think?
  3. Why do you think programmers chose two equals signs for “is it equal?” What other signs would you have picked?

Questions kids ask

What is if else in programming?

It’s a decision structure: if checks a condition, and if it’s true, its instructions run; else runs different instructions when the condition is false. Almost every programming language has something like it.

What is a Boolean?

A kind of information that has only two values: true and false (True and False). Every condition produces a Boolean value. The name comes from George Boole, a 19th-century mathematician who studied the logic of true and false.

What is elif in Python?

It’s short for “else if.” You use it when there are more than two options — for example, a score that’s excellent, good or not enough. The computer checks the conditions in order and runs only the first one that’s true.

🔎 Ask Thinkapedia another question