🔒
Unlock Everything
£7.99/month
or £59/year
Subscribe now →
💻 Component 2 · 2.2 Programming
2.2.1c Sequence, Selection & Iteration
OCR J277 · GCSE Computer Science · ~12 min read
Notes
Video
Slides
Worksheet
Quiz

The Three Programming Constructs

Every algorithm can be written using just three fundamental constructs. These are the building blocks of all programs in OCR J277.

Sequence
Instructions executed one after another, in order
Selection
A decision — different code runs depending on a condition
Iteration
Repetition — a block of code runs multiple times

1. Sequence

The simplest construct. Lines of code execute one after another, top to bottom, in the order they are written. There is no branching or repetition.

// Sequence example — runs in order, line by line
name = input("Enter name: ")
age = int(input("Enter age: "))
OUTPUT "Hello, " + name
OUTPUT "You are " + str(age) + " years old"

2. Selection — IF…THEN…ELSE

Selection uses a condition to decide which branch of code to execute. The condition evaluates to True or False.

// Simple IF
IF score >= 50 THEN
    OUTPUT "Pass"
END IF

// IF…ELSE
IF score >= 50 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
END IF

// IF…ELSE IF…ELSE (nested)
IF score >= 70 THEN
    OUTPUT "Distinction"
ELSE IF score >= 50 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
END IF

3. Iteration — FOR Loops (Count-Controlled)

A FOR loop repeats a block a known, fixed number of times. The loop counter increments automatically.

// FOR loop — runs exactly 5 times (1, 2, 3, 4, 5)
FOR i = 1 TO 5
    OUTPUT i
NEXT i

// FOR loop counting down
FOR i = 10 TO 1 STEP -1
    OUTPUT i
NEXT i

4. Iteration — WHILE Loops (Condition-Controlled)

A WHILE loop repeats while a condition is True. The condition is tested before each iteration. If the condition is False at the start, the loop body never executes.

// WHILE loop — repeats until password is correct
password = ""
WHILE password != "secret"
    password = input("Enter password: ")
END WHILE
OUTPUT "Access granted"

5. Iteration — DO…UNTIL Loops

A DO…UNTIL loop always executes at least once, because the condition is checked after each iteration. Continues until the condition becomes True.

// DO…UNTIL — always runs at least once
DO
    answer = int(input("Guess (1-10): "))
UNTIL answer == 7
OUTPUT "Correct!"

Comparing the Loops

Loop typeWhen to useCondition checkedMin. runs
FORKnown number of iterationsBefore each iteration0 (if range empty)
WHILEUnknown iterations; check firstBefore each iteration0 (if condition false)
DO…UNTILMust run at least onceAfter each iteration1 always

Nested Constructs

Constructs can be nested inside each other — e.g. a FOR loop inside a FOR loop (2D iteration), or an IF inside a WHILE loop.

// Nested FOR loops — multiplication table
FOR i = 1 TO 3
    FOR j = 1 TO 3
        OUTPUT i * j
    NEXT j
NEXT i
Exam tip: Key exam questions on this topic: (1) "Name the programming construct that..." — know sequence/selection/iteration definitions cold. (2) "Which loop would you use when the number of iterations is not known?" — WHILE or DO…UNTIL. (3) "What is the difference between WHILE and DO…UNTIL?" — WHILE checks condition first (may run 0 times); DO…UNTIL checks after (always runs at least once). (4) Writing pseudocode: always use correct OCR J277 syntax with END IF, END WHILE, NEXT.
⚠️ Common Mistakes
  • Forgetting END IF, END WHILE, or NEXT i — these close the block; always include them
  • Using a FOR loop when the number of iterations is unknown — use WHILE or DO…UNTIL
  • Confusing WHILE (check first) and DO…UNTIL (check after) — a WHILE may run 0 times
  • Off-by-one errors in FOR loops: FOR i = 1 TO 5 runs 5 times (1,2,3,4,5) not 4
  • Writing IF without END IF — always close your selection block
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.2.1c Sequence, Selection & Iteration

8 questions · 20 marks

Q1Name the three programming constructs and give a one-sentence definition of each.[3]
✅ Mark scheme
Sequence: instructions executed one after another in order [1]. Selection: a condition is evaluated and different code runs depending on whether it is True or False (IF…THEN…ELSE) [1]. Iteration: a block of code is repeated — either a fixed number of times (FOR) or while a condition holds (WHILE/DO…UNTIL) [1].
Q2Write pseudocode using a FOR loop to output the numbers 1 to 10.[2]
✅ Mark scheme
FOR i = 1 TO 10 [1]; OUTPUT i; NEXT i [1]. Award marks for correct loop bounds and closing NEXT statement.
Q3Explain the difference between a WHILE loop and a DO…UNTIL loop.[2]
✅ Mark scheme
A WHILE loop tests the condition before each iteration — if the condition is False from the start, the body never executes (0 runs possible) [1]. A DO…UNTIL loop tests the condition after each iteration — the body always runs at least once [1].
Q4Write pseudocode to ask the user to enter a number between 1 and 10. Keep asking until they enter a valid number.[3]
✅ Mark scheme
DO [1]; num = int(input("Enter a number (1-10): ")); UNTIL num >= 1 AND num <= 10 [2]. Accept WHILE equivalent with appropriate condition. Award 1 mark for loop structure, 1 for input, 1 for correct condition.
Q5Write pseudocode to output grades based on a score. 70+ = A, 50-69 = B, below 50 = C.[4]
✅ Mark scheme
IF score >= 70 THEN [1]; OUTPUT "A" [1]; ELSE IF score >= 50 THEN; OUTPUT "B" [1]; ELSE; OUTPUT "C"; END IF [1]. Must use nested IF/ELSE IF structure. Award marks for correct thresholds and output.
Q6How many times does this loop execute? FOR i = 3 TO 9 STEP 2[2]
✅ Mark scheme
4 times [1]: i = 3, 5, 7, 9 [1]. STEP 2 means the counter increases by 2 each iteration.
Q7A game loop should keep running while the player has lives > 0 and the game is not won. Write the WHILE condition.[2]
✅ Mark scheme
WHILE lives > 0 AND gameWon == False [1] (or NOT gameWon) [1]. Both conditions must be present with AND. Award 1 for each correct condition.
Q8A FOR loop inside another FOR loop is called a nested loop. How many times does the inner body run in: FOR i = 1 TO 4; FOR j = 1 TO 3; [body]; NEXT j; NEXT i?[2]
✅ Mark scheme
12 times [1]. The inner loop (j: 1 to 3) runs 3 times per outer iteration. The outer loop (i: 1 to 4) runs 4 times. Total: 4 × 3 = 12 [1].
?
out of 20 — self-mark above
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 10
Click to reveal definition
🎉
Complete!
TermDefinition
🎯

Mini Test — 2.2.1c Sequence, Selection & Iteration

10 questions · 10 marks · 10 minutes

← 2.2.1b Data Types 2.2 Programming 2.2.1d Arrays →