By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Grade 5 Computer Science Study Guide: Conditionals in Python (if-else)
"Imagine you’re programming a robot to deliver snacks in your school. If the snack is hot chocolate, the robot should say ‘Careful, it’s warm!’—but if it’s a granola bar, it should just say ‘Enjoy!’ How do you teach the robot to make that decision every single time, without you telling it what to do next?"
Think of an if-else statement like a fork in the hallway at school. You walk down the hall, and when you reach the fork, you check a sign: "If the sign says ‘Library,’ turn left. Else, turn right." The robot (or your Python program) does the same thing—it checks a condition (like "Is the snack hot chocolate?"), and based on whether that condition is True or False, it takes one path or the other.
if-else
Here’s how it works in code:
snack = "hot chocolate" if snack == "hot chocolate": print("Careful, it's warm!") else: print("Enjoy!")
The == is like asking, "Is this exactly the same as that?" If the answer is True, the code under if runs. If False, the code under else runs. There’s no in-between—just like you can’t go both left and right at the fork.
==
if
else
Key Vocabulary:- Conditional: A statement that makes a decision based on whether something is True or False. Example: A traffic light checks "Is the light red?" before deciding to stop cars.- Boolean: A value that is either True or False (like a yes/no question). Example: "Is 5 greater than 3?" → True. "Is ‘apple’ == ‘banana’?" → False.- Indentation: The spaces at the start of a line in Python that show which code belongs to the if or else. Example: In a recipe, indented steps might say "If using butter, do this first"—the indentation groups the steps together.- Equality Operator (==): The symbol that asks, "Are these two things the same?" Example: "dog" == "cat" is False, but "dog" == "dog" is True.
"dog" == "cat"
"dog" == "dog"
How this appears in class:- Exit Ticket: Write a 3-line Python program that prints "Too cold!" if the temperature is below 60, and "Perfect!" otherwise. Use temp = 55. - Proficient response: python temp = 55 if temp < 60: print("Too cold!") else: print("Perfect!") - Developing response: Forgets the else or uses = instead of ==. Example: python if temp = 55: # Wrong operator! print("Too cold!") - What the teacher looks for: Correct syntax (== or <), proper indentation, and a working else.
temp = 55
python temp = 55 if temp < 60: print("Too cold!") else: print("Perfect!")
=
python if temp = 55: # Wrong operator! print("Too cold!")
<
State standardized test framing (if applicable):- Multiple-choice questions might show a code snippet and ask, "What will this print?" with distractors like: - Printing both messages (ignoring the if-else logic). - Printing nothing (forgetting the else). - Using the wrong comparison (e.g., temp > 60 instead of <).
temp > 60
Model student response (proficient):Prompt: Write code that prints "Go outside!" if it’s sunny, and "Stay inside" if it’s raining. Use weather = "sunny".
weather = "sunny"
weather = "sunny" if weather == "sunny": print("Go outside!") else: print("Stay inside")
Why it’s good: Uses == correctly, indents properly, and covers both cases.
Mistake 1: Using = instead of ==- Question: What will this code print? python age = 10 if age = 12: print("You're 12!") else: print("You're not 12.") - Common wrong response: Thinks it will print "You're not 12" (or gets an error and guesses randomly).- Why it loses credit: = is for assigning values (e.g., age = 10), while == is for comparing them. Python will throw an error here.- Correct approach: Use == to compare. The code should be: python if age == 12: # Fixed! print("You're 12!") else: print("You're not 12.")
python age = 10 if age = 12: print("You're 12!") else: print("You're not 12.")
age = 10
python if age == 12: # Fixed! print("You're 12!") else: print("You're not 12.")
Mistake 2: Forgetting the else- Question: Write code that prints "Pass" if a score is 70 or above, and "Try again" otherwise. Use score = 65.- Common wrong response: python score = 65 if score >= 70: print("Pass") print("Try again") # Oops! This runs no matter what. - Why it loses credit: The print("Try again") isn’t connected to the if, so it runs every time. The else is missing.- Correct approach: Add else to handle the "Try again" case: python if score >= 70: print("Pass") else: print("Try again")
score = 65
python score = 65 if score >= 70: print("Pass") print("Try again") # Oops! This runs no matter what.
print("Try again")
python if score >= 70: print("Pass") else: print("Try again")
Mistake 3: Indenting incorrectly- Question: What’s wrong with this code? python time = 8 if time < 9: print("Good morning!") # No indentation! else: print("Good afternoon!") - Common wrong response: Thinks the code will run fine (or guesses it prints nothing).- Why it loses credit: Python uses indentation to group code under if/else. Without it, the code crashes.- Correct approach: Indent the print line: python if time < 9: print("Good morning!") # Fixed! else: print("Good afternoon!")
python time = 8 if time < 9: print("Good morning!") # No indentation! else: print("Good afternoon!")
print
python if time < 9: print("Good morning!") # Fixed! else: print("Good afternoon!")
Within CS: Conditionals → Loops Why it matters: Loops (like while) use conditionals to decide when to keep going. For example, "While the snack is hot, keep cooling it" is just a conditional checked over and over.
while
Across subjects: Conditionals → Scientific hypotheses Why it matters: A hypothesis is like an if statement: "If I water this plant daily, then it will grow taller." The experiment tests whether the condition ("water daily") leads to the outcome ("grow taller").
Outside school: Conditionals → Board games Why it matters: In Monopoly, you check "If I land on Boardwalk, pay $400 rent." The game’s rules are a series of if-else statements—just like your Python code!
"What happens if you write an if without an else? Is that ever useful, or is it always a mistake?"
Pointer toward the answer: An if without an else is like a fork in the hallway where one path just… ends. It’s not a mistake—it’s useful when you only want to do something if a condition is true, and do nothing otherwise. For example:
if battery < 20: print("Low battery! Plug in your laptop.")
Here, you don’t need an else because you don’t care what happens if the battery is not low. But if you do care about both cases (like the snack robot), you need the else. The key is: Does the program need to make a choice between two actions, or just check one thing?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.