Algorithms and programming · GCSE Computer Science

Sequence, selection and iteration

GCSE Computer Science programming constructs: sequence, IF selection, WHILE and FOR iteration, and how nested decisions and loops are read in exam algorithms.

UNDERSTANDRETRIEVEREMEMBER
THE MEMORY HOOK
Sequence is one statement after another. Selection is IF. Iteration is WHILE or FOR. Nested means an IF or a loop sitting inside another — indent it and dry-run the inner block first.

The important bits

What you need to know

  1. 1

    Every program is built from sequence (statements in order), selection (choose a path) and iteration (repeat). Naming the construct is often a 1-mark gift on design questions.

  2. 2

    Sequence is the default: line 1, then line 2. If you swap two assignments, later lines see the wrong values. Trace tables assume this order; so does the CPU.

  3. 3

    Selection uses IF / ELSE IF / ELSE (or CASE / SWITCH on some boards). A condition is Boolean: true or false. Only one branch runs. Nested IF is still selection, not a loop.

  4. 4

    WHILE is condition-controlled iteration: the body may run zero times if the condition is already false. You must change something inside the loop or you have an infinite loop.

  5. 5

    FOR is count-controlled: you know how many repeats, or you walk a known range. REPEAT…UNTIL (where the spec includes it) runs the body at least once, then tests.

  6. 6

    Nested loops: the inner loop finishes all of its repeats for each single pass of the outer loop. A 3-by-4 nest is 12 inner-body runs, not 7.

  7. 7

    Nested selection inside a loop is the usual exam cocktail: WHILE more data, IF valid then process ELSE reject. Indentation (or END IF / END WHILE) shows which statements belong where.

  8. 8

    Pick the construct from the English: “keep going until QUIT” is WHILE, not FOR, because the count is unknown. “For every student in the file” is FOR or FOR EACH.

Quotations worth analysing

Short evidence. Real method.

Sequence, selection and iteration are the three basic programming constructs.
GCSE Computer Science computational thinking / programming

Name all three if the question says “identify the constructs”. Giving only “loops” misses sequence and selection, which are separate marks.

A WHILE loop tests the condition before the body is executed.
Mark-scheme iteration phrase

Zero passes are allowed. REPEAT…UNTIL tests after the body, so it always runs at least once. Mixing those two is a standard 2-mark trap.

Nested means one construct is placed inside another.
Definition used on structured-programming items

Say what is inside what: an IF inside a WHILE, or a FOR inside a FOR. “The code is nested” without naming the pair is too vague.

Go deeper

Selection versus iteration: pick the tool from the verbs

Selection chooses a path once: if age < 18, refuse the ticket. Iteration repeats until a condition fails or a count completes: keep asking for a PIN while it is wrong, or FOR i FROM 0 TO n−1 totalling an array. Nested IF is not a loop; a WHILE that never changes its condition is an infinite loop. When a question describes “keep going until the user types QUIT”, that is WHILE, not FOR, because you do not know the count in advance. When it says “do this for every student in the file”, that is a counted or FOR EACH loop. Naming the construct in a 2-mark design question is free credit if you have rehearsed the verbs: until, while, for each, if. Write the condition so it can become false, and show the statement that updates it.

Go deeper

Nested blocks are read inside-out on a dry-run

FOR row ← 1 TO 3, FOR col ← 1 TO 2, OUTPUT row, col. The inner loop runs fully for row 1 (two outputs), then fully for row 2, then row 3 — six lines, not five. Students add the bounds and write 5. The same mistake happens with IF inside WHILE: the IF is decided every pass, so the branch can change. Indent or use END IF so you do not attach an ELSE to the wrong IF. In a trace table, the inner loop counter should reset each time the outer loop ticks. If the paper’s pseudocode is poorly indented, still follow the END WHILE / END IF keywords — they are the legal structure. Draw a small box around the inner block on the question paper; it is the cheapest way not to execute it in the wrong place.

WORKED EXAMPLE

See the idea in action

A PIN check. attempts ← 0 locked ← FALSE WHILE attempts < 3 AND locked = FALSE INPUT pin IF pin = 2468 THEN OUTPUT "Open" locked ← TRUE ELSE attempts ← attempts + 1 ENDIF ENDWHILE Inputs: 1111, 0000, 2468. Pass 1: 1111 ≠ 2468, attempts = 1, locked still FALSE. Pass 2: 0000 ≠ 2468, attempts = 2. Pass 3: 2468 = 2468, output Open, locked TRUE. WHILE now fails on locked = TRUE. Sequence is the order of those lines; selection is the IF; iteration is the WHILE. Nested IF sits inside the loop. A FOR loop from 1 TO 3 would also cap attempts, but a correct PIN on try 1 must still be allowed to stop early — WHILE (or a flag inside FOR) matches that English.

Exam technique

Turn knowledge into marks

Name the construct from the scenario before you write code: IF for a one-off decision, WHILE when you do not know the count, FOR when you do. On nested questions, tick the inner END first.

Common mistakes

Do not give these marks away

  1. 01

    Using FOR when the number of repeats is unknown, or WHILE when the question says “exactly 10 times” and a count is sitting there.

  2. 02

    Treating nested IF as a loop, or adding the two loop bounds instead of multiplying them.

  3. 03

    Writing a WHILE whose condition never changes, then being surprised the trace table never ends.

QUICK RETRIEVAL

A program must keep asking for a password until it is correct. Which construct is the best fit?

ASequence only, with no condition

BA WHILE (or REPEAT) loop, because the number of attempts is not known in advance

CA FOR loop that always runs exactly twice

DAn IF without any iteration

Show the answer

A WHILE (or REPEAT) loop, because the number of attempts is not known in advance. You cannot name the count before the user types. WHILE tests a condition (password wrong) and repeats. A single IF would check once; a fixed FOR would stop even if still wrong, or keep going after success unless you add a flag.

Quick questions

If this is the bit you searched

What is the difference between a FOR loop and a WHILE loop GCSE?

FOR is count-controlled: you know how many times, or you iterate a known range. WHILE is condition-controlled: it may run zero times, and you must make sure the condition can become false.

What does nested mean in programming?

One construct is written inside another, such as an IF inside a WHILE, or a loop inside a loop. The inner block completes according to its own rules on each pass of the outer block.

Does a WHILE loop always run at least once?

No. The condition is tested first. If it is false, the body is skipped. REPEAT…UNTIL (if your board uses it) runs the body first, then tests.

How do I show sequence, selection and iteration in an exam answer?

Sequence: numbered steps in order. Selection: IF with a Boolean condition and both branches if needed. Iteration: WHILE or FOR with a sensible condition or count. Label them if the question asks you to identify constructs.