Computer Science
Grade 5
20 min
If-Else Statements
If-Else Statements
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Explain what a nested if-else statement is and why it's useful.
Write a program using an `else if` (or `elif`) to check for multiple, separate conditions.
Combine two conditions into a single `if` statement using the `AND` operator.
Combine two conditions into a single `if` statement using the `OR` operator.
Trace the logic of a complex conditional statement to predict its output.
Debug a simple program that has an error in its nested or combined conditional logic.
Ever wonder how a video game character knows to use a special power only when their health is low AND they have enough magic? 🤔 Let's learn how to give our code super-smart decision-making powers!
We already know how to make simple choices with `if-else`. Now, we'll learn how t...
2
Key Concepts & Vocabulary
TermDefinitionExample
Nested If-Else StatementAn if-else statement that is placed inside another if-else statement. It's like a decision inside another decision.First, we check `if it is the weekend`. If it is, we then check a second, nested question: `if the weather is sunny`.
Else If (elif)A way to check for another specific condition if the first `if` condition was false. You can chain many `else if`s together to check for lots of different possibilities.`if grade >= 90`, print 'A'. `else if grade >= 80`, print 'B'. `else`, print 'C'.
Logical OperatorA special word like `AND` or `OR` that lets you connect two or more true/false conditions into one big condition.To check if you can go to the party, you might need to know `if (finished_homework AN...
3
Core Syntax & Patterns
Nested If-Else Structure
if condition1:
if condition2:
# Code to run if both are true
else:
# Code to run if 1 is true, 2 is false
else:
# Code to run if condition1 is false
Use this when one decision depends on the outcome of another. The indentation (the spaces) is very important to show which `if` and `else` belong together.
The 'Else If' Chain
if conditionA:
# Do task A
elif conditionB:
# Do task B
elif conditionC:
# Do task C
else:
# Do a default task
Use this instead of many separate `if` statements when the conditions are related and only one outcome should happen. The computer checks them in order and stops at the first one that is true.
Combining Conditions
if (condition1 AND condition2):
# Do something...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
The code `if is_raining: if temperature < 50: print('Wear a heavy raincoat.')` can be rewritten as a single `if` statement. Which option is equivalent?
A.if is_raining OR temperature < 50:
B.if is_raining AND temperature < 50:
C.if is_raining: print('Wear a heavy raincoat.')
D.if is_raining elif temperature < 50:
Challenging
Why is an `if-elif-else` chain often better than using multiple, separate `if` statements for checking a student's letter grade (A, B, C, D)?
A.It runs faster because it stops checking after it finds the first true condition.
B.It can check for more conditions than separate `if` statements.
C.It's the only way to use the `>` and `<` operators.
D.Separate `if` statements can sometimes print multiple grades for one score.
Challenging
This code has two errors for opening a secret door: it should open if `is_wizard` is true OR if `level > 20`. Find the code with both errors fixed.
`if is_wizard AND level > 20`
`print('Door Opens')`
A.if is_wizard OR level > 20:
print('Door Opens')
B.if is_wizard AND level >= 20:
print('Door Opens')
C.if is_wizard OR level > 20
print('Door Opens')
D.if is_wizard AND level > 20:
print('Door Opens')
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free