GCSE · Computer Science · AQA · Spec 8525
Nested selection
Why would a program ask for your password if it doesn't even recognise your username?
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 go back and 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. Step through and 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 is only ever asked on one branch.
What you need to know
- Nested selection is a selection block placed inside another selection block.
- Any kind of selection block can be nested: an IF inside an IF, an IF-ELSE inside an IF, or an IF with ELIF and ELSE branches inside an IF.
- A condition nested inside an IF is only checked when that IF's condition is True. If the outer condition is False, the nested block is skipped entirely and the program carries on after the outer block, or in its else branch.
- Nesting builds more complex branching logic: it gives a program more possible paths, and you can nest more than one level deep.
- In Python, indentation decides which block a line belongs to, so every nested statement and its code must be indented to the correct level.
- You need to be able to trace nested code (which lines run and what is output for given inputs) and modify it.
The big picture
Nested selection means putting one selection block inside another, like an IF inside an IF. A condition nested inside an IF is only checked when that IF's condition is True; if it is False, the whole nested block is skipped. Nesting gives a program more possible paths to follow, and it can go more than one level deep. In Python, indentation is what tells the computer 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. Where 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 30 September 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.