← VCE Software Development U3O1

Nested if statements in Python

An if statement indented inside another if block. The inner condition is only checked when the outer condition is True.

Understanding Nested if statements in Python in VCE Software Development U3O1

What it does: The extra indentation on the inner if shows it sits inside the outer if block. It only runs when logged_in is True. temp > 20 is False, so Python skips the whole outer block and never tests temp > 30. Nothing is printed. Two nested ifs with no else can be combined into one if using and. Both conditions must be True for the code to run.

Common mistakes: Python's Boolean value is capitalised: True. Lowercase true is an undefined name, so the outer condition causes a NameError and the nested if never runs. It should be: if logged_in == True:. The keyword is misspelled. The outer statement must start with if, otherwise the nested if inside it has no valid outer block. It should be: if mark >= 50:.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 10–47) and word search (Modules 10–71).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
When is the inner if checked in a nested if statement?
Every time the program runsOnly when the outer condition is True
Matches
if age >= 18: if has_id: print("Enter")
↓
The inner if is not indented, so it is not inside the outer if block. Python raises an IndentationError because the outer block is empty.
Applying
Meanings
Term
nested if
Meaning
An if statement indented inside another if block. The inner condition is only checked when the outer condition is True.
Practice
present = True
Testing
Gaps
haslicence:
Check Answer
Spots
1if logged_in == true:
2 if score > 50:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Woolly Mammoth' badge — Tundra Master, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. When is the inner if checked in a nested if statement?
A. Every time the program runs
B. Only when the outer condition is True ✓
The inner if sits inside the outer if block, so Python only reaches it when the outer condition is True.
2. Which code shows a nested if?
A. if age >= 18: if has_id: print("Enter") ✓
B. if age >= 18: if has_id: print("Enter")
The inner if must be indented one extra level inside the outer if block. Without the indentation it is not nested (and gives an IndentationError).
3. score = 40 if score > 50: if score > 80: print("High") What does this code print?
A. High
B. Nothing ✓
The outer condition 40 > 50 is False, so the inner if is never checked and nothing is printed.
4. How do you show that an if statement is inside another if block?
A. Indent it one extra level ✓
B. Put it in brackets ( )
Python uses indentation to show which code belongs to which block. The inner if is indented one extra level (usually 4 spaces) further than the outer if.
5. Which statement about nested if and the and operator is correct?
A. if a: followed by an inner if b: can act like if a and b: ✓
B. A nested if can never be written using and
If the inner if has no else or elif, nesting if a then if b behaves the same as if a and b. Nesting is useful when you need extra steps after the outer check.

Match the Pairs

if age >= 18: if has_id: print("Enter") → A nested if: the inner if has_id is only checked when the outer condition age >= 18 is True
if score >= 50 and attempts <= 3: print("Pass") → A single if using and: the same result as a nested if, but both conditions are joined on one line
The inner if is indented one extra level (4 spaces) so Python knows it belongs inside the outer if block → if logged_in: if is_admin: print("Admin panel")
If the outer condition is False, the inner if is skipped and never checked → temp = 15 if temp > 20: if temp > 30: print("Hot")
if age >= 18: if has_id: print("Enter") → The inner if is not indented, so it is not inside the outer if block. Python raises an IndentationError because the outer block is empty.
if x > 0: if x < 10 print("Small") → The inner if is missing the colon at the end of its condition, which causes a SyntaxError.

Key Term

nested if
An if statement indented inside another if block. The inner condition is only checked when the outer condition is True.

Fill in the Blanks

1. if age >= 18: if has_licence: print("Can drive")
Hint: Type the keyword that starts the inner condition, which is only checked when the outer one is True.
2. if score >= 50: if score >= 90: print("Distinction")
Hint: Type the comparison operator that means greater than or equal to.
3. if age >= 18 and has_licence: print("Can drive")
Hint: Type the logical operator that joins both conditions on one line, doing the same job as a nested if.
4. if x > 0: if x % 2 == 0: print("Positive even")
Hint: Type the function that displays text, inside the innermost block.
5. if mark >= 50: if mark == 100: print("Perfect score")
Hint: Type the operator that tests whether two values are equal.

Spot the Error

Question 1
A if logged_in == true:
B if score > 50:
C print('Pass')
Line A — Python's Boolean value is capitalised: True. Lowercase true is an undefined name, so the outer condition causes a NameError and the nested if never runs. It should be: if logged_in == True:
Question 2
A iff mark >= 50:
B if mark >= 85:
C print('Top mark')
Line A — The keyword is misspelled. The outer statement must start with if, otherwise the nested if inside it has no valid outer block. It should be: if mark >= 50:
Question 3
A if age >= 18:
B if has_licence:
C print('Can drive'
Line C — The print call is missing its closing bracket. It should be: print('Can drive'), which only runs when both the outer and inner conditions are True.
Question 4
A if temp > 30:
B if humidity => 70:
C print('Humid')
Line B — => is not a valid comparison operator in Python. The inner condition should use >=, or > as in the correct program: if humidity > 70:
Question 5
A if score >= 50:
B if score >= 80
C print('Merit')
Line B — The inner if statement is missing its colon. Every if line, including a nested one, must end with a colon: if score >= 80:

Student with a class code? Join your class →