GCSE · Computer Science · Edexcel · Spec 1CP2

Selection

Type the right password and you're in; type the wrong one and you're not. Same program both times. The only difference is which lines run. That's selection.

Computer Science · Selection

Guess the number: which way does the program go?

The same program runs twice: once with a right guess, once with a wrong one. Press Next and watch which box lights up at the diamond, and which one doesn't.

Predict first: when the guess is 7, which Output box never runs?

TrueFalseStartsecret = 7Input guessguess == secret?Output "You got it!"Output "Unlucky!"Output "Game over"End

Output

 

Step 1: Run 1. A player is about to take a guess.

1 / 14

Use Next to follow the program one step at a time. Run 1 guesses 7; run 2 guesses 4.

Step 1 of 14: Run 1. A player is about to take a guess..

Exam line: At a decision, say three things: the condition, whether it is True or False for this input, and which path is taken.
Watch out: Selection doesn't end the program. After the True box or the False box, both paths join up again and the next step runs either way.

Computer Science · Conditions

True or False? Test the conditions

score = 50. Does each condition evaluate to True or False?

Still to sort

True (0)

The comparison holds when score is 50.

Where the line is: With >= and <=, 'or equal to' is built in, so the boundary value itself makes them True.

False (0)

The comparison does not hold when score is 50.

Where the line is: > and < are strict: 50 is not greater than 50, and not less than 50.

7 of 7 still to sort.

Every diamond holds a condition, and every condition comes out True or False. Here, score = 50. Read each condition out loud as a question, then sort it.

Watch out: The boundary is where marks go. At score = 50, > and >= give different answers, and so do < and <=.

Computer Science · Operators

Spot the broken line

Here's the guessing game from the flowchart, written in Python by a student. The diamond has become if, and the False arrow has become else. One line uses the wrong operator. Which one?

A student's program — which line goes wrong?

Computer Science · Algorithms

Trace table — dry run the code

Three possible outputs means three paths, so this program uses if, then elif (short for 'else if'), then else. Step through three runs, mark = 85, then 55, then 30, and predict each output first.

1mark = int(input("Mark: "))
2if mark >= 70:
3 print("Distinction")
4elif mark >= 50:
5 print("Merit")
6else:
7 print("Not yet")
8print("Done")
mark
condition
result
Output
Press Start to run the first line.
·

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

Computer Science · Selection

Does the order matter?

A student writes the grading program with the first two conditions swapped. It checks if mark >= 50: print("Merit") first, then elif mark >= 70: print("Distinction"). There is no else. The user enters 85.

What does this program print? Pick the idea closest to what you think right now.
How sure are you?

Exam line: When tracing if/elif/else, stop at the first True condition. Anything after it is not checked.

Computer Science · Nested selection

A decision inside a decision

Walk each route. On which route is the password never checked at all?

Outer check: username → Inner check: password

3 possible routes through the log-in program.

On Log-in attempt. 2 branches to choose from.

The program: if username == "sam": then, indented inside it, if password == "pa55": print("Welcome") else: print("Wrong password"). The outer else: print("Unknown user").

Watch out: Indentation shows what is inside what. The password check is indented inside the username block, so it belongs to the username True route only.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

How a program checks a condition and decides which code to run.

What you need to know

  • Selection is used when a program has more than one possible path: it checks a condition and runs a block of code depending on whether the condition is True or False, so some lines never run.
  • A condition is an expression that evaluates to True or False. It is built with a relational operator: == (equal to), != (not equal to), < (less than), <= (less than or equal to), > (greater than) or >= (greater than or equal to).
  • In a flowchart, selection is a diamond-shaped decision symbol holding the condition, with one path labelled True and the other False.
  • In Python, the block under if runs only when its condition is True; an else block runs when it is False. After the selection statement, the program carries on with the next line.
  • With if, elif and else, conditions are checked one by one and only the block for the first True condition runs. You can use any number of elif blocks, and else is optional.
  • In Python, == compares two values for equality; a single = assigns a value to a variable.
  • Nested selection puts one selection statement inside another. An inner if placed in the outer if's block is only checked when the outer condition is True.

The big picture

Selection lets a program choose between paths. It checks a condition, an expression that is either True or False, and runs a block of code depending on the answer, so some lines are skipped. In a flowchart the choice is a diamond with True and False paths; in Python it is written with if, elif and else. Conditions are built with the relational operators ==, !=, <, <=, > and >=. In an if/elif/else, conditions are checked in order and only the first True condition's block runs. Selection statements can also be nested one inside another.

Key points

1Selection = check a condition, then take exactly one path. Some lines are skipped.
2A condition is always True or False.
3Flowchart decision = diamond, with True and False paths that join up again afterwards.
4if runs its block when True; else runs its block when False; then the next line runs.
5if/elif/else: checked top to bottom, first True wins, else is optional.
6== compares, = assigns.
7Nested: the inner check inside an if's block is only reached when the outer condition is True.
8To trace: for each input, record each condition checked, True or False, which block runs and the output.

Worked example

Problem

Write a Python program that asks for a temperature (a whole number) and prints "Hot" if it is above 25, "Cold" if it is below 10, and "Mild" otherwise. Then test it with 30, 5 and 25.

⚠ Watch out

Thinking that every True condition in an if/elif/else gets its block run. Only the first True condition's block runs; the rest are skipped, even ones that would also be True. So the order of the conditions matters.

🧠

Memory hook

One question, one path, first True wins. And don't forget: one = stores, two == compare.

✓

Check yourself

n = 10. Is n >= 10 True or False? What about n > 10? And if the if and the first elif are both True for an input, which block runs?

Flashcards

(15)
What is selection?
Choosing which path a program takes: it checks a condition and runs a block of code depending on whether it's True or False, so some lines are skipped.
What is a condition?
An expression that evaluates to True or False.
How is selection shown in a flowchart?
A diamond-shaped decision symbol containing the condition, with one path labelled True and the other False.
When does the block under an if statement run?
Only when the if's condition is True.
When does an else block run?
When none of the conditions above it are True.
What happens after a selection statement finishes?
The program carries on with the next line after it, whichever path was taken.
In if/elif/else, several conditions are True. Which block runs?
Only the block for the first True condition. Conditions are checked in order, top to bottom.
Does an if statement need an else?
No, else is optional. Without one, if no condition is True, no block runs and the program carries on.
How many elif blocks can an if statement have?
Any number.
In Python, what is the difference between = and ==?
= assigns a value to a variable. == compares two values and gives True if they are equal.
What does != mean?
Not equal to. It's True when the two values are different.
x = 5. Is x > 5 True or False? What about x >= 5?
x > 5 is False (5 isn't greater than 5). x >= 5 is True (5 is equal to 5).
What is nested selection?
A selection statement placed inside the block of another selection statement.
An if is nested inside another if's block. When is the inner condition checked?
Only when the outer condition is True. If the outer condition is False, the inner if is never reached.
How do you trace a selection program for a given input?
Check each condition in order, note whether it's True or False, and record which lines run and what is output.

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 Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)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.