Programming Concepts · Cambridge syllabus 8.1 · Lesson 4 of 4
Sequence, Selection and Iteration
Programs combine sequence, selection and iteration. Sequence establishes values, selection handles cases, and iteration processes repeated work. Check how these structures nest and whether loop updates happen on every required path.
By the end, you can
- Implement sequential instructions.
- Implement IF/ELSE logic.
- Use nested conditions.
- Combine conditions with logical operators.
- Implement FOR loops.
- Implement WHILE loops.
- Implement REPEAT UNTIL loops.
- Select the correct loop structure.
Sequence
Sequence in a program is the default: statements execute one after another, top to bottom, each exactly once. Everything else is a deliberate departure from it.
Because order is meaning, a correct set of statements in the wrong order is a wrong program. A variable must be assigned before it is read; a file must be opened before it is written to and closed after; a total must be reset before a loop adds to it; and a value must be validated before it is used, not after.
The dependencies between statements are what fix the order, and they are worth tracing when a program misbehaves for no visible reason. If a line uses a value, the line that produces that value must already have run.
Sequence errors are the easiest kind to find by hand, because they are visible in a trace table: a variable holds the wrong value at a particular step, and the step before it is the one at fault.
Worth remembering
- Statements run top to bottom, once each, unless something says otherwise.
- A statement that uses a value must come after the one that produces it.
- A trace table exposes an ordering mistake at the step where a value is wrong.
Selection
Selection in a program is written with IF, and the condition inside it evaluates to a Boolean. IF Score >= 50, with THEN on the next line, the block and ENDIF, runs the block only when the condition is true; adding ELSE gives the alternative. CASE OF chooses between many values of one variable, with OTHERWISE for the rest.
Nesting handles graded outcomes. 0478 pseudocode has no ELSE IF: to turn a mark into a grade, put the next IF inside the ELSE of the one before, each with its own ENDIF, and test the highest boundary first, because a mark of 92 satisfies every condition below it and would otherwise be caught by the wrong one. Each branch must be reachable, and a final ELSE should catch everything that no earlier condition did. The syllabus will not ask for more than three levels of nesting.
Conditions combine with AND, OR and NOT. AND is true only when both parts are; OR when at least one is; NOT reverses. IF Age >= 13 AND Age <= 19 tests a range, and it needs both comparisons written out in full: Age >= 13 AND <= 19 is not valid, and 13 <= Age <= 19 is not allowed either.
Brackets settle ambiguity, and the syllabus advises using them in any complex condition. Write IF Member = TRUE OR (Age >= 60 AND Day = "Monday") so there is no doubt which parts go together.
The recurring errors are using = for assignment where comparison was meant, testing conditions in the wrong order, and forgetting the boundary value, since >= and > differ by exactly the case a test at the boundary would catch.
Worth remembering
- IF, THEN, ELSE and ENDIF; nest a second IF inside the ELSE, highest boundary first.
- AND needs both sides true, OR needs one; write each comparison out in full.
- Bracket combined conditions rather than relying on precedence.
Iteration
Iteration repeats statements, and choosing the loop is a matter of one question: is the number of repetitions known in advance?
A FOR loop is count controlled and is the right choice when it is. FOR i ← 1 TO 10, block, NEXT i sets up, increments and tests the counter automatically, so a loop that must run a fixed number of times cannot accidentally run a different number. Processing every element of an array of known length is the standard use, and the counter usually doubles as the index.
A WHILE loop tests its condition before the block, so the block may run zero times. It is right when the repetition depends on something that may already be satisfied: WHILE Attempts < 3 AND NOT Correct DO.
A REPEAT UNTIL loop tests after the block, so the block always runs at least once. It is right for input validation, because the question has to be asked before the answer can be judged.
Two errors recur. An infinite loop, where nothing inside the block changes the value the condition depends on — a WHILE Count < 10 whose body never increases Count. And an off-by-one error, where a list of ten items is processed 1 TO 9 or 0 TO 10; the fix is to be certain whether the first index is 0 or 1 in the language being used, and to test with a list of one item.
Loops nest: an inner loop completes in full on every single pass of the outer one, which is how a grid is processed and why a nested loop over a hundred items does ten thousand iterations.
Worth remembering
- FOR when the count is known; WHILE when it may be zero; REPEAT when it is at least one.
- Something inside the loop must be able to make the condition false.
- An inner loop runs completely on every pass of the outer loop.
Worked example
A loop over marks can use an IF inside it to count only marks at or above a threshold.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
Initialise Count to 0, loop over the marks, and increment Count when the current mark is >= 50.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.