Scribe logoScribeNotes & practice
Sign inStart free
  1. Notes
  2. ›IB Computer Science
  3. ›B2 Programming
  4. ›B2.3 Programming constructs

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.

  • 5 min read
  • 5 parts
  • 10 questions
  • Draft
Start a Quick 5 on this topic→Jump to the 10 questions
1/5Every program is built from three constructs
  1. 1Every program is built from three constructs
  2. 2Selection uses a condition to choose a path
  3. 3Loops repeat actions until a stopping point is reached
  4. 4Nesting places one construct inside another
  5. 5Tracing nested code means following one pass at a time
  6. Check your recall
  7. Practice: 10 questions
1

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

ConstructUse it whenExample
SequenceSteps must happen in a fixed orderRead two numbers, then add them
SelectionAn action depends on the dataGive a discount only if the total is high enough
IterationAn action must be repeatedAdd up every mark in a class
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 1 done

2

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 mark >= 50\texttt{mark >= 50}mark >= 50. An if-else statement runs one block when the condition is true and another when it is false.

For example, if mark >= 50 then output "Pass" else output "Fail" end if\texttt{if mark >= 50 then output "Pass" else output "Fail" end if}if mark >= 50 then output "Pass" else output "Fail" end if prints exactly one word. Use else if to test several bands in turn. The first true condition runs, and the rest are skipped.

Common mistake

Do not write = to compare values. In these notes = assigns a value, and ==\texttt{==}== compares two values.

Key term

condition

A condition is an expression that evaluates to true or false and controls which statements run.

Part 2 done

Quick check

One question on the part you just read

Easy

Which construct runs statements one after another in the order they are written?
Show answer and explanation

Answer: B · Worked explanation

Sequence is the default construct, in which each statement runs once in the order written. Selection chooses between paths using a condition, and iteration repeats statements. Nesting places one construct inside another, so it describes how constructs combine rather than how single statements run.
3

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, loop i from 1 to 4\texttt{loop i from 1 to 4}loop i from 1 to 4 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 while n > 0\texttt{while n > 0}while n > 0. The number of repeats is not known in advance. It may even be zero.

Figure 1. Selection chooses one of two paths and then continues. Iteration returns to the test until the condition fails.

Worked example

Tracing a count-controlled loop

Trace this program and state its output. total = 0; loop i from 1 to 4; total = total + i; end loop; output total\texttt{total = 0; loop i from 1 to 4; total = total + i; end loop; output total}total = 0; loop i from 1 to 4; total = total + i; end loop; output total
  1. 1
    When i is 1, total becomes 0+1=10+1=10+1=1.
  2. 2
    When i is 2, total becomes 1+2=31+2=31+2=3.
  3. 3
    When i is 3, total becomes 3+3=63+3=63+3=6.
  4. 4
    When i is 4, total becomes 6+4=106+4=106+4=10.
  5. 5
    The loop ends and the program outputs total.

Answer: The output is 10.

Common mistake

Something inside a while loop must change its condition. Otherwise the loop never ends.

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 3 done

4

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

An outer loop runs 3 times. Each time, an inner loop runs 4 times. Calculate how many times the inner body runs.
  1. 1
    Each outer pass runs the inner loop 4 times.
  2. 2
    There are 3 outer passes.
  3. 3
    Multiply: 3×4=123\times4=123×4=12.

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.

Part 4 done

Quick check

One question on the part you just read

Easy

Which construct repeats a group of statements?
Show answer and explanation

Answer: C · Worked explanation

Iteration repeats statements, using a loop. Sequence runs statements once in order, and selection runs one block or another depending on a test. A condition is only the true-or-false expression that selection or a while loop uses to decide, so it does not repeat anything by itself.
5

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

Trace this program and state its output. count = 0; loop i from 1 to 6; if i mod 2 == 0 then count = count + i; end if; end loop; output count\texttt{count = 0; loop i from 1 to 6; if i mod 2 == 0 then count = count + i; end if; end loop; output count}count = 0; loop i from 1 to 6; if i mod 2 == 0 then count = count + i; end if; end loop; output count
  1. 1
    The loop gives i the values 1 to 6.
  2. 2
    The if test is true only when i is even, so only 2, 4 and 6 are added.
  3. 3
    count becomes 0+2=20+2=20+2=2, then 2+4=62+4=62+4=6, then 6+6=126+6=126+6=12.
  4. 4
    The output is the final value of count.

Answer: The output is 12.

Trace table for the loop

ii mod 2 == 0count after
1false0
2true2
3false2
4true6
5false6
6true12
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

Exam tip

Show every pass in your trace table. Marks are often given for correct intermediate values as well as the final output.

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.

Note read

Check your recall[14 marks]

6 written questions. Answer each one in your head or on paper, then open the mark scheme.

  1. 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. 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. 3.Determine[3 marks]

    Determine the output of this program. loop i from 1 to 2; loop j from 1 to 3; output i * j; end loop; end loop\texttt{loop i from 1 to 2; loop j from 1 to 3; output i * j; end loop; end loop}loop i from 1 to 2; loop j from 1 to 3; output i * j; end loop; end loop

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

    5×4=205\times4=205×4=20 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.

3.

Easy[1 mark]

Which construct should be used when an action depends on whether a condition is true?
Show answer and explanation

Answer: B · Worked explanation

Selection runs statements only when a condition is true, or chooses between two blocks. Sequence cannot make that choice, since it always runs every statement. Iteration repeats statements, which is a different need. Concatenation joins strings together and is not a programming construct.
4.

Medium·Determine[1 mark]

Determine the output of this program. x = 10; if x > 5 then x = x - 3 else x = x + 3 end if; output x\texttt{x = 10; if x > 5 then x = x - 3 else x = x + 3 end if; output x}x = 10; if x > 5 then x = x - 3 else x = x + 3 end if; output x
Show answer and explanation

Answer: B · Worked explanation

The condition 10 > 5 is true, so the first branch runs and x becomes 10−3=710-3=710−3=7. The value 13 would come from the else branch, which is skipped. The value 10 assumes that nothing changes. The value 3 does not come from either branch.
5.

Medium·Determine[1 mark]

Determine the output of this program. p = 1; loop i from 1 to 4; p = p * i; end loop; output p\texttt{p = 1; loop i from 1 to 4; p = p * i; end loop; output p}p = 1; loop i from 1 to 4; p = p * i; end loop; output p
Show answer and explanation

Answer: D · Worked explanation

The value of p becomes 1×1=11\times1=11×1=1, then 1×2=21\times2=21×2=2, then 2×3=62\times3=62×3=6, then 6×4=246\times4=246×4=24. The value 10 comes from adding the values of i instead of multiplying. The value 16 is 4×44\times44×4, and 120 would need a fifth pass of the loop.
6.

Medium[1 mark]

A program must keep asking for a password until the correct one is entered. Which loop suits this best?
Show answer and explanation

Answer: B · Worked explanation

The number of attempts cannot be known in advance, so the loop must repeat while the password is wrong, which is a condition-controlled loop. The length of the password does not decide how many attempts are made. A single selection tests once, and a fixed sequence cannot repeat until success.
7.

Medium·Calculate[1 mark]

An outer loop runs 6 times. Each time, an inner loop runs 3 times. Calculate how many times the inner body runs.
Show answer and explanation

Answer: D · Worked explanation

The inner loop runs in full on each outer pass, so the inner body runs 6×3=186\times3=186×3=18 times. The value 9 comes from adding the two counts instead of multiplying. The value 6 counts only the outer passes. The value 36 squares the outer count, which is not how nesting works.
8.

Hard·Determine[1 mark]

Determine the output of this program. count = 0; loop i from 1 to 6; if i mod 3 == 0 then count = count + 1 end if; end loop; output count\texttt{count = 0; loop i from 1 to 6; if i mod 3 == 0 then count = count + 1 end if; end loop; output count}count = 0; loop i from 1 to 6; if i mod 3 == 0 then count = count + 1 end if; end loop; output count
Show answer and explanation

Answer: C · Worked explanation

The test i mod 3 == 0 is true only for i = 3 and i = 6, so count increases twice and the output is 2. The value 3 is the first value of i that passes the test. The value 6 is the number of times the loop runs, not the number of times the test is true.
9.

Hard·Determine[1 mark]

Determine the output of this program. n = 10; count = 0; while n > 0; n = n - 4; count = count + 1; end while; output n, count\texttt{n = 10; count = 0; while n > 0; n = n - 4; count = count + 1; end while; output n, count}n = 10; count = 0; while n > 0; n = n - 4; count = count + 1; end while; output n, count
Show answer and explanation

Answer: C · Worked explanation

Trace each pass. Start with n = 10 and count = 0. Pass 1 gives 6 and 1. Pass 2 gives 2 and 2. Since 2 > 0 is true, pass 3 gives −2 and 3. Now −2 > 0 is false, so the loop stops. Stopping after pass 2 gives 2 and 2, which ignores that last pass.
10.

Hard·Determine[1 mark]

Determine the output of this program. total = 0; loop i from 1 to 3; loop j from 1 to 3; if i == j then total = total + i end if; end loop; end loop; output total\texttt{total = 0; loop i from 1 to 3; loop j from 1 to 3; if i == j then total = total + i end if; end loop; end loop; output total}total = 0; loop i from 1 to 3; loop j from 1 to 3; if i == j then total = total + i end if; end loop; end loop; output total
Show answer and explanation

Answer: C · Worked explanation

The test i == j is true only when i and j match, which happens at 1, 2 and 3. Each time, total gains the value of i, so total is 1+2+3=61+2+3=61+2+3=6. The value 3 counts how many times the test is true instead of adding i. The value 9 counts how many times the inner body runs.

Keep going

Next topicB2.4 Programming algorithmsPrevious topicB2.2 Data structures

More in B2 · Programming

  1. B2.1Programming fundamentalsNotes · 10 questions
  2. B2.2Data structuresNotes · 10 questions
  3. B2.3Programming constructsNotes · 10 questions
  4. B2.4Programming algorithmsNotes · 10 questions
  5. B2.5File processingNotes · 10 questions

All IB Computer Science topics →

Draft: written by Scribe and checked automatically, not yet by a subject reviewer.

Practice questions are original, written by Scribe and checked automatically for correctness.

Topic codes follow the official numbering. Not endorsed by IB Diploma Programme or any exam board.

On this page

0/5 read

  1. 1Every program is built from three constructs
  2. 2Selection uses a condition to choose a path
  3. 3Loops repeat actions until a stopping point is reached
  4. 4Nesting places one construct inside another
  5. 5Tracing nested code means following one pass at a time
  6. Check your recall
  7. Practice: 10 questions
All coursesScribeHow it worksPricingPrivacyTerms

Notes and questions are written by Scribe in our own words. Topic codes follow the official course numbering so you can find your place. Not produced or endorsed by the IB, College Board, AQA, OCR, Pearson Edexcel or any exam board.

© 2026 Scribe