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.

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. Before each step, guess the next line number. Then 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 type ada and then hello, and it prints Loading your files... anyway. Which line is in the wrong place?

A student's program — which line goes wrong?

Modify the program

Add a PIN check and a guest login

Upgrade 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 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

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. Any type of selection block can go inside.
The outer condition is False. What happens to the nested block?
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?
The program goes inside the outer block and checks the conditions nested there.
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 deep 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. How far in does it go?
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 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.