Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Selection

Nested IF

All topicsPractise exam questions
Knowledge organiser

Selection

301.9c

IF statements inside IF statements.

What you need to know

A nested if is an if statement inside another if statement. Use this when a second decision depends on the first. Indentation shows which if each else belongs to.

Key points

  • An if inside an if is 'nested'
  • The inner if only runs if the outer if is True
  • Indentation shows the nesting level
  • Don't nest too deeply — consider elif instead

Code examples

Nested IF
python
has_ticket = True
age = 14

if has_ticket:
    if age >= 18:
        print("Adult entry")
    else:
        print("Child entry")
else:
    print("Buy a ticket first")