IB Computer Science HL · Year 2 · Lesson 4

Sequence and Selection

Construct Python programs whose instruction order and selection logic match the stated objective.

Paper 250 minutesB2.3.1 · B2.3.2
Today’s targets

What you need to be able to do

2 / 10
  • construct programs whose instructions execute in a correct sequence;
  • explain how instruction order can affect functionality and output;
  • construct Python selection using if, elif and else;
  • use Boolean and relational operators to control program flow;
  • recognize control-flow failures such as incorrect output, infinite loops and deadlock.
B2.3.1B2.3.2
Paper 2 habitRead the command term and the data given. A technically correct answer still needs to answer the exact task.
Learn + retrieve

Rapid Recall Deck

3 / 10

Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.

B2.3.1 · Construct

Sequence: order changes behaviour

4 / 10
GoalRead a price, reject negative values, apply a 10% discount to valid prices, then print the final price.

Incorrect order

price = -20
price = price * 0.90
print(price)

if price < 0:
    print("Invalid price")

Repair the sequence

Rewrite the code so an invalid price is not discounted or printed as a valid result.

Validate first — the program must decide whether the input is valid before calculating a discounted price.
Only valid data should reach the calculation and final output.
price = -20

if price < 0:
    print("Invalid price")
else:
    price = price * 0.90
    print(price)
B2.3.2 · Selection

Choose exactly the branch that should run

5 / 10
score = 78

if score >= 90:
    band = "A"
elif score >= 75:
    band = "B"
elif score >= 60:
    band = "C"
else:
    band = "D"

print(band)

Trace before revealing

Predict the printed output for score = 78. Then explain why the final elif score >= 60 branch does not also run.

Order — evaluate the conditions from the top of the chain.
Selection — identify the first branch whose condition is true.
Control flow — explain what happens to the remaining branches after one branch is selected.

The output is B. The first condition is False, the second is True, so the program assigns band = "B" and skips the remaining elif and else blocks.

Conditions

Boolean and relational operators

6 / 10

Relational operators

< · <= · > · >= · == · !=

Each comparison produces a Boolean value.

Boolean operators

and requires both conditions to be True.

or requires at least one to be True.

not reverses a Boolean value.

Predict four conditions

Given age = 17, has_id = True, score = 82, predict each expression.

age >= 18
score >= 75 and has_id
age >= 18 or score > 80
not has_id
  1. False
  2. True
  3. True
  4. False
Construct

Build a complete selection structure

7 / 10
RequirementA school lab grants access when a user is a staff member, or when the user is a student who is at least 16 and has completed safety training.

Construct the Python decision

Assume these variables already exist: is_staff, is_student, age, trained.

Use Boolean operators to reflect the two allowed routes.
Print "Access granted" or "Access denied".
if is_staff or (is_student and age >= 16 and trained):
    print("Access granted")
else:
    print("Access denied")
Avoid control-flow failures

Incorrect output, infinite loops and deadlock

8 / 10

Incorrect output

Instructions execute in an order that calculates or displays the wrong value.

Infinite loop

A repetition condition never becomes False, so the program does not terminate normally.

Deadlock

Two or more tasks wait indefinitely for one another or for resources held by one another.

Diagnose the failure type

Task A repeatedly checks while attempts < 3 but never changes attempts. Task B locks resource X then waits for Y, while another task locks Y then waits for X.

Task A — name the control-flow failure and state the missing change.
Task B — name the concurrency/resource failure and explain why progress stops.

Task A: infinite loop; attempts must be updated so the condition can eventually become False. Task B: deadlock; each task waits for a resource held by the other, so neither can proceed. A consistent resource-acquisition order is one way to avoid this circular wait.

Exam lens

Teacher-created Paper 2 practice

9 / 10

1. Given temperature = 31 and raining = False, construct an if/elif/else structure that prints "Indoor" when it is raining, "Heat plan" when it is not raining and temperature is at least 30, otherwise "Outdoor".

2. Explain why checking an input for validity after using it in a calculation can produce incorrect output.

3. Given x = 5, state the Boolean value of x != 5 or x >= 10.

  1. if raining:
        print("Indoor")
    elif temperature >= 30:
        print("Heat plan")
    else:
        print("Outdoor")
  2. The calculation may use invalid data and produce a value before the program rejects the input. Validation should occur before the dependent calculation.
  3. False.
Homework

Finish the learning cycle

10 / 10

IA — main task

Review the IA plan and identify one place where the order of processing steps matters. Make sure the planned sequence is explicit enough that another programmer could follow it.

Syllabus — short retrieval

Redo the access-control construct on page 7 from memory. Then make one new test case that should be granted and one that should be denied.