GCSE · Computer Science · AQA · Spec 8525
Nested selection
Why would a program ask for your password when it doesn't even recognise your username? Good news: a well-built one doesn't. Here's how it knows not to.
Nested selection · Follow the route
Can you reach the password check with the wrong username?
Pick True or False at each check and follow the login program to its output. Then the challenge: try to reach the password check with a wrong username.
Outer check: the username → Inner check: the password
Every run starts with one question: is this a username the program knows?
3 possible routes through the program.
An IF-ELSE on its own gives a program 2 routes. Nest a second IF-ELSE inside its True branch and there are 3, because only that branch splits again.
Computer Science · Algorithms
Trace table — dry run the code
Now the real code behind the tree. Three runs: A types ada then c0de42, B types ada then hello, C types bob. Before each step, guess the next line number. Then watch which line numbers turn up in the table, and which never do.
Ready when you are — step through one line at a time.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
An IF inside an IF, and why the inner question only ever gets asked on one branch.
What you need to know
- Nested selection is a selection block placed inside another selection block.
- Any type can go inside: an IF in an IF, an IF-ELSE in an IF, or an IF with ELIF and ELSE in an IF.
- The outer condition is checked first. If it's True, the program steps inside and checks the nested conditions.
- If the outer condition is False, the whole nested block is skipped. The program carries on after the outer block, or in its else.
Have a goA school-trip program checks if form_signed is True. Nested inside, it checks if packed_lunch is True. Your mate forgot the form but packed an amazing lunch. Does the lunch check ever happen?
No. The program never even looks at the lunch.
The form condition is False, so the whole nested block is skipped. Thinking the lunch still gets checked is reading every IF on the page instead of following the route.
- Nesting is how you build complex branching logic: it gives a program more possible paths to follow.
- You can keep going: more than one level deep, and more than one nested block, for more complex conditions.
Have a goAn IF-ELSE gives 2 routes. This time, nest an IF-ELSE inside BOTH of its branches. How many routes now?
4
Each branch splits in two, so 2 + 2 = 4. The answer 3 is only right when just one branch has something nested in it.
- In Python, indentation decides which block a line belongs to.
- So every nested statement, and the code inside it, must be indented to exactly the right level.
Have a goLine 1: if a: Line 2 (4 spaces): if b: Line 3 (8 spaces): print("X") Line 4 (4 spaces): print("Y"). Which print belongs to the if b block?
print("X") on line 3
Line 3 is one level further in than if b, so it's inside it. Line 4 is level with if b, so it belongs to the if a block only, even though it comes after if b.
- Tracing means working out which lines run, and what is output, for given inputs.
- Modifying means editing the program to change what it does, such as adding a branch or a further nested check.
The big picture
Nested selection is one selection block inside another, like an IF inside an IF. The outer condition is checked first: if it's True, the program goes inside and checks the nested conditions; if it's False, the whole nested block is skipped. Nesting gives a program more possible paths, and it can go more than one level deep. In Python, indentation decides which block each line belongs to.
Key points
Worked example
Problem
A ride program first checks height >= 120. Only inside that True branch does it ask for the rider's age and check age >= 12: True prints Ride alone, False prints Ride with an adult. The outer else prints Too short to ride. What is output for (a) height 135 and age 9, and (b) height 110 and age 15?
⚠ Watch out
Checking every IF you can see. If the outer condition is False, the inner condition is never asked, so don't trace into the nested block at all.
Memory hook
Doors inside rooms: you can only try an inner door once you're through the outer one. Turned away at the front door? You never even see the inner doors.
Check yourself
In the login program, is there any username and password that makes it print both Wrong password and Unknown user? Why not?
Flashcards
(12)What is nested selection?
Name three kinds of selection block that can be nested inside an IF.
The outer condition is False. What happens to the nested block?
The outer condition is True. What happens next?
Why use nested selection?
Can selection be nested more than one level deep?
In Python, what decides which block a line belongs to?
A print line should run only when an inner condition is True. How far in does it go?
What does tracing nested code mean?
In the login program, what has to be True to get Access granted?
What does modifying a program with nested selection mean?
You nest an IF-ELSE inside ONE branch of an IF-ELSE. How many routes are there now?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 28 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.