πŸ“˜ CodingMarble Learn

Control Flow: Sequence, Selection and Iteration

A program is a list of instructions. Control flow is the order in which they run. There are only three building blocks: sequence (one after another), selection (IF: pick one path) and iteration (loops: repeat). Loops are definite (FOR: count known) or indefinite (WHILE / REPEAT UNTIL: stop when a condition changes). Blocks can sit inside each other (nesting). Values are kept in variables (can change) and constants (fixed), each with a clear, meaningful name.

🎬 Step-by-step story

  1. Sequence. The red token is the computer. It runs each line in order: x ← 4, then y ← x Γ— 2, then shows y.
  2. Variables and constants. r is a variable: its value can change. PI is a constant (purple): set once, never changed. The arrow ← means store.
  3. Selection. The yellow diamond asks a Yes/No question: is mark β‰₯ 50? Only ONE path is taken. Move the mark slider and run again.
  4. Definite loop. FOR i ← 1 TO 5 repeats a fixed number of times. Count the turns: 1, 2, 3, 4, 5, then it stops.
  5. Indefinite loop. WHILE total < 20 keeps adding 6. We did not know the count before; it stopped after 4 turns when the condition became False.
  6. Nesting and free play. An IF sits inside a FOR loop, so each turn asks 'even or odd?'. Change n and press Run again.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

πŸ€” Common doubts, cleared

Why does the order of lines matter?

The computer only knows a value after the line that stores it has run. In step 1, y can only be worked out after x ← 4 has run.

What does the arrow ← mean? Is it 'equals'?

It means 'store the value on the right into the name on the left'. It is a command, not a maths statement, so x ← x + 1 makes sense.

Why use a constant instead of just typing 3.14 everywhere?

You change it in one place, it cannot be changed by mistake, and the name PI tells the reader what the number means.

Can both the IF and the ELSE parts run?

No. The condition is either True or False, so the token takes exactly one road. Try marks above and below 50 in step 3.

When should I use FOR and when WHILE?

If you can say 'repeat exactly n times' before you start, use FOR. If you say 'repeat until something happens', use WHILE or REPEAT…UNTIL. Compare steps 4 and 5.

Why did the WHILE loop end at 24 and not 20?

The check happens before each turn. At 18 the answer to 'total < 20?' was Yes, so 6 more was added, giving 24. Then the check said No.

How does the IF inside the loop work?

On every turn the loop reaches the IF and asks the question again with the new value of i. That is nesting. See step 6.

Variables, constants and assignment

A variable is a named place in memory that holds a value. The value can change while the program runs. A constant also has a name and a value, but the value is fixed for the whole program, for example CONST VAT ← 0.2.

Assignment means storing a value in a variable. In pseudocode we write score ← 10 (in Python: score = 10). The right side is worked out first, then stored on the left, so score ← score + 1 adds one.

Meaningful identifiers

An identifier is the name you give a variable, constant or subroutine. Good names say what the value is: totalPrice is better than tp or x. Clear names make code easier to read, fix and share. Names cannot have spaces and usually cannot start with a digit.

Why use constants? If the tax rate changes, you edit one line, and you cannot change it by mistake later in the program.

Sequence: one step after another

Sequence means instructions run in the order they are written, top to bottom. Changing the order can change the answer.

x ← 4
y ← x Γ— 2
OUTPUT y      β†’ 8

If you swap the first two lines, y is worked out before x has a value, so the program fails or gives a wrong answer.

Selection: IF, ELSE and CASE

Selection lets a program choose. A condition is a question that is either True or False, such as mark β‰₯ 50.

IF mark β‰₯ 50 THEN
   OUTPUT 'Pass'
ELSE
   OUTPUT 'Try again'
ENDIF

Only one branch runs. For more than two choices use ELSE IF (Python elif), or a CASE/match statement that checks one value against a list. Conditions use comparison operators (=, β‰ , <, >, ≀, β‰₯) and can be joined with AND, OR, NOT.

Iteration: definite and indefinite loops

Iteration means repeating. It saves writing the same lines again and again.

Definite (count-controlled) loop

The number of repeats is known before the loop starts. Use FOR.

FOR i ← 1 TO 5
   OUTPUT i
ENDFOR        β†’ 1 2 3 4 5

Indefinite (condition-controlled) loop

The loop repeats until a condition changes, so the number of turns is not known in advance.

total ← 0
WHILE total < 20
   total ← total + 6
ENDWHILE
OUTPUT total  β†’ 24

Typical uses: asking for a password until it is right, reading input until the user types 'stop'. Danger: if the condition never becomes False you get an infinite loop.

Nesting

Nesting means putting one control structure inside another: an IF inside a loop, a loop inside a loop, or an IF inside an IF. Indentation shows what is inside what.

FOR i ← 1 TO 4
   IF i MOD 2 = 0 THEN
      OUTPUT i, ' even'
   ELSE
      OUTPUT i, ' odd'
   ENDIF
ENDFOR

A loop inside a loop multiplies the work: an outer loop of 3 and an inner loop of 4 run the inner body 3 Γ— 4 = 12 times. This is how you print a times-table grid or walk through every cell of a 2D array.

Try it: trace table at home

Take a sheet of paper and draw columns for each variable. Run the WHILE example from this page line by line, writing each new value in a new row. Then check your last row with the 3D at step 5. Next, change the rule to total ← total + 7: predict how many turns it takes, then test it in the 3D idea or in any free online Python editor.

Key formulas and definitions

Worked examples

1. What does this output? a ← 3; b ← a + 4; a ← b Γ— 2; OUTPUT a, b

Line by line: a = 3. b = 3 + 4 = 7. a = 7 Γ— 2 = 14. Output: 14 7.

2. Write pseudocode that outputs 'Adult' if age is 18 or more, otherwise 'Child'.

INPUT age; IF age β‰₯ 18 THEN OUTPUT 'Adult' ELSE OUTPUT 'Child' ENDIF. Only one message is shown, because selection takes one branch.

3. How many times does the body run? FOR k ← 2 TO 10

Count from 2 to 10 inclusive: 10 βˆ’ 2 + 1 = 9 times.

4. Trace: n ← 1; WHILE n < 50: n ← n Γ— 3. What is n at the end and how many turns?

1 β†’ 3 (turn 1) β†’ 9 (turn 2) β†’ 27 (turn 3) β†’ 81 (turn 4). 81 < 50 is False, so it stops. n = 81 after 4 turns.

5. Nested: FOR r ← 1 TO 3, inside FOR c ← 1 TO 4, OUTPUT '*'. How many stars?

Inner loop runs 4 times for each of the 3 outer turns: 3 Γ— 4 = 12 stars.

6. Write a loop that keeps asking for a PIN until it equals 4321. Which loop type and why?

REPEAT INPUT pin UNTIL pin = 4321. It is indefinite (we do not know how many tries) and the user must be asked at least once, so REPEAT…UNTIL fits best.

Common mistakes

Practice quiz

1. Which structure chooses between two paths?
2. A loop where the number of repeats is known in advance is:
3. A value that cannot change while the program runs is a:
4. How many times does FOR i ← 1 TO 7 run?
5. Which loop always runs its body at least once?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are the three basic programming constructs?

Sequence, selection and iteration. Every program, however big, is built from these three.

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

A FOR loop repeats a known number of times (definite). A WHILE loop repeats as long as a condition is True, so the count may not be known (indefinite).

What is an infinite loop?

A loop whose stopping condition never becomes true, so it runs forever. It usually happens when the loop variable is not updated.

Where this is taught

England (GCSE, A level)Year 103.2 Programming

Learn first

Learn next

Related lessons

All Computer Science lessons