IB Computer Science · B2 · Programming
B2.3 Programming constructs
Identify when to use sequence, selection and iteration, write if-else statements and loops, and trace nested loops and conditionals.
1/5Every program is built from three constructs
Part 1 of 5: Every program is built from three constructs
A recipe lists steps in order, makes choices and repeats actions. Programs are built the same way, using three constructs.
Sequence runs statements one after another in the order written. Selection uses a true-or-false test to choose which statements run. Iteration repeats statements until a stopping point is reached.
When to use each construct
- Construct
- Sequence
- Use it when
- Steps must happen in a fixed order
- Example
- Read two numbers, then add them
- Construct
- Selection
- Use it when
- An action depends on the data
- Example
- Give a discount only if the total is high enough
- Construct
- Iteration
- Use it when
- An action must be repeated
- Example
- Add up every mark in a class
Most programs mix all three. Choose each construct from what the task needs.
Key terms · tap one for its definition
sequence
Sequence is the construct in which statements run one after another in the order in which they are written.
selection
Selection is the construct in which a condition decides which statements are run.
iteration
Iteration is the construct in which a group of statements is repeated.
Part 2 of 5: Selection uses a condition to choose a path
Selection needs something to decide with. That is a condition.
A condition is an expression that gives true or false, such as . An if-else statement runs one block when the condition is true and another when it is false.
For example, prints exactly one word. Use else if to test several bands in turn. The first true condition runs, and the rest are skipped.
Key term
condition
A condition is an expression that evaluates to true or false and controls which statements run.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: B · Worked explanation
Part 3 of 5: Loops repeat actions until a stopping point is reached
Selection chooses once. Often the same step must happen many times.
A count-controlled loop repeats a set number of times. In these notes, runs its body four times, with i taking the values 1, 2, 3 and 4.
A condition-controlled loop repeats while a condition stays true, as in . The number of repeats is not known in advance. It may even be zero.
Worked example
Tracing a count-controlled loop
- When i is 1, total becomes .
- When i is 2, total becomes .
- When i is 3, total becomes .
- When i is 4, total becomes .
- The loop ends and the program outputs total.
Answer: The output is 10.
Key terms · tap one for its definition
count-controlled loop
A count-controlled loop is a loop that repeats its body a fixed number of times.
condition-controlled loop
A condition-controlled loop is a loop that repeats its body for as long as a condition remains true.
Part 4 of 5: Nesting places one construct inside another
Real programs rarely use one construct alone. A loop often contains a selection, or another loop.
Nesting means putting one construct inside another. The inner construct runs in full each time the outer one runs once.
A selection inside a loop is just as common. For example, a loop can check each mark in turn and count only the marks that pass. Indentation in code shows which statements sit inside which construct.
A clock shows the idea. The minutes count from 0 to 59 in full for every single hour. The total number of inner passes is the outer count multiplied by the inner count.
Worked example
Counting executions in nested loops
- Each outer pass runs the inner loop 4 times.
- There are 3 outer passes.
- Multiply: .
Answer: The inner body runs 12 times.
Key term
nesting
Nesting is the placing of one programming construct inside another, such as a loop inside a loop or a selection inside a loop.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: C · Worked explanation
Part 5 of 5: Tracing nested code means following one pass at a time
Nested code is easiest to follow with a table. A trace table records the value of each variable after every line of a program is executed. Finish all inner passes before the outer loop moves on.
Check each condition against the current values, not the original ones. The operator mod gives the remainder after division, so i mod 2 is 0 when i is even.
Worked example
Tracing a selection inside a loop
- The loop gives i the values 1 to 6.
- The if test is true only when i is even, so only 2, 4 and 6 are added.
- count becomes , then , then .
- The output is the final value of count.
Answer: The output is 12.
Trace table for the loop
- i
- 1
- i mod 2 == 0
- false
- count after
- 0
- i
- 2
- i mod 2 == 0
- true
- count after
- 2
- i
- 3
- i mod 2 == 0
- false
- count after
- 2
- i
- 4
- i mod 2 == 0
- true
- count after
- 6
- i
- 5
- i mod 2 == 0
- false
- count after
- 6
- i
- 6
- i mod 2 == 0
- true
- count after
- 12
Key term
trace table
A trace table is a table that records the value of each variable after every line of a program is executed.
Check your recall[14 marks]
6 written questions. Answer each one in your head or on paper, then open the mark scheme.
- 1.Distinguish[2 marks]
Distinguish between selection and iteration.
Show mark schemeHide mark scheme
Model answer
Selection uses a condition to choose which statements run, so each path runs at most once. Iteration repeats statements until a stopping point is reached.
- 1 mark for stating that selection chooses which statements run using a condition
- 1 mark for stating that iteration repeats statements
- 2.Outline[2 marks]
Outline when a condition-controlled loop is more suitable than a count-controlled loop.
Show mark schemeHide mark scheme
Model answer
It is more suitable when the number of repeats is not known in advance. For example, a program can keep asking for a password until it is correct.
- 1 mark for stating that the number of repeats is not known in advance
- 1 mark for giving a suitable example, such as repeating until valid input is entered
- 3.Determine[3 marks]
Determine the output of this program.
Show mark schemeHide mark scheme
Model answer
For i = 1 the inner loop outputs 1, 2, 3. For i = 2 it outputs 2, 4, 6. The output is 1, 2, 3, 2, 4, 6.
- 1 mark for finding 1, 2, 3 for the first outer pass
- 1 mark for finding 2, 4, 6 for the second outer pass
- 1 mark for giving the values in the correct order
- 4.Write down[3 marks]
Write down an if-else statement that outputs “Adult” when age is 18 or more, and “Minor” otherwise.
Show mark schemeHide mark scheme
Model answer
if age >= 18 then output "Adult" else output "Minor" end if
- 1 mark for using the condition age >= 18
- 1 mark for outputting “Adult” in the true branch
- 1 mark for outputting “Minor” in the else branch
- 5.Explain[2 marks]
Explain why the body of an inner loop usually runs more times than the body of the outer loop.
Show mark schemeHide mark scheme
Model answer
The inner loop runs in full on every pass of the outer loop. The inner body therefore runs the outer count multiplied by the inner count.
- 1 mark for stating that the inner loop runs completely for each outer pass
- 1 mark for stating that the total is the outer count multiplied by the inner count
- 6.Calculate[2 marks]
Calculate how many times the inner body runs when an outer loop runs 5 times and its inner loop runs 4 times on each pass.
Show mark schemeHide mark scheme
Model answer
times.
- 1 mark for multiplying the outer count by the inner count
- 1 mark for giving 20

Practice this topic
10 quick-fire multiple-choice questions, easy to hard. Tap an answer to see if you're right and the worked explanation. (The written questions with mark schemes are under “Check your recall” above.)
0 right0 of 10 answered
Includes the 2 quick checks in the note above.
Easy[1 mark]
Show answer and explanation
Answer: B · Worked explanation
MediumDetermine[1 mark]
Show answer and explanation
Answer: B · Worked explanation
MediumDetermine[1 mark]
Show answer and explanation
Answer: D · Worked explanation
Medium[1 mark]
Show answer and explanation
Answer: B · Worked explanation
MediumCalculate[1 mark]
Show answer and explanation
Answer: D · Worked explanation
HardDetermine[1 mark]
Show answer and explanation
Answer: C · Worked explanation
HardDetermine[1 mark]
Show answer and explanation
Answer: C · Worked explanation
HardDetermine[1 mark]
Show answer and explanation
Answer: C · Worked explanation