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.

On A username is typed in. 2 branches to choose from.

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.

1username = input("Username: ")
2if username == "ada":
3 password = input("Password: ")
4 if password == "c0de42":
5 print("Access granted")
6 else:
7 print("Wrong password")
8else:
9 print("Unknown user")
Run
Line
username
password
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Check your thinking

What happens to the password check?

Same login program. The outer check is if username == "ada": and the password check is nested inside it. Someone types the username bob.

Which is closest to what you think happens to the password check?
How sure are you?

Indentation is the nesting

Find the line in the wrong block

This program should print Loading your files... only when BOTH the username and the password are right. But when someone types ada and then hello, it prints Loading your files... anyway. Which line is wrong?

A student's program — which line goes wrong?

Modify the program

Add a PIN check and a guest login

Change the login program in two ways. (1) After a correct password, ask for a PIN: print Access granted only if the PIN is 7391, otherwise print Wrong PIN. (2) Let the username guest in with the message Guest access. Build the new program line by line.

  1. username = input("Username: ") if username == "ada": password = input("Password: ") if password == "c0de42":Lines 1 to 4 stay exactly as they were.
  2. missing step
Which line is step 2?

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

1Nested = selection inside selection.
2Outer condition False: skip everything inside it. The inner conditions are never even asked.
3Outer condition True: go in, then check the inner conditions one level at a time.
4More nesting, more routes. An IF-ELSE gives 2 routes; nest another IF-ELSE in one branch and you get 3.
5Indentation is the nesting. Move a line left or right and it belongs to a different block.
6To trace, follow ONE route: note each line that runs and each output.

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?
A selection block placed inside another selection block, for example an IF inside an IF.
Name three kinds of selection block that can be nested inside an IF.
An IF, an IF-ELSE, or an IF with ELIF and ELSE branches.
The outer condition is False. What happens to the nested block?
It is skipped entirely: none of its conditions are checked and none of its lines run. The program carries on after the outer block, or in its else branch.
The outer condition is True. What happens next?
Execution moves inside the outer block, and the conditions nested there are checked.
Why use nested selection?
To build complex branching logic. It gives a program more possible paths to follow.
Can selection be nested more than one level deep?
Yes. You can keep nesting, more than one level and more than one nested block, to set more complex conditions for when code runs.
In Python, what decides which block a line belongs to?
Its indentation. Each nested block is indented one level further than the line that opens it.
A print line should run only when an inner condition is True. Where does it go?
Indented one level deeper than that inner if, so it sits inside the inner block.
What does tracing nested code mean?
Working out, for given inputs, which lines execute and what is output, by following the one route the conditions choose.
In the login program, what has to be True to get Access granted?
Both conditions: the username check and the password check nested inside it.
What does modifying a program with nested selection mean?
Editing the existing code to change what it does, for example adding an ELIF branch or a further nested check.
You nest an IF-ELSE inside ONE branch of an IF-ELSE. How many routes are there now?
Three. The branch you nested into splits in two; the other branch is still one route.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

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.