Cambridge O Level Computer Science · Syllabus 2210 · Programming
Iteration
What is Iteration?
A control structure in which a block of statements is executed repeatedly; Cambridge pseudocode provides count-controlled FOR loops, pre-condition WHILE loops and post-condition REPEAT loops.
This definition is part of the Programming chapter in Cambridge O Level Computer Science.
Iteration in context
A program is a stored sequence of instructions that manipulates data held in named storage. Topic 8 asks you to do four things with that idea. First, name and type your data correctly: a variable whose value may change, a constant whose value may not, each with one of the five data types INTEGER, REAL, CHAR, STRING, BOOLEAN. Second, control the order in which instructions run using only three structures — sequence, selection (IF, CASE) and iteration (FOR, WHILE, REPEAT). Third, break a long solution into procedures and functions so it stays readable and testable. Fourth, hold many related values in an array and hold them beyond the end of the run in a file. Everything else in this chapter is detail hanging off those four moves.
Iteration means a block of statements is executed repeatedly. Cambridge pseudocode has exactly three iteration structures. A count-controlled loop (FOR ... NEXT) runs a known number of times, driven by an INTEGER loop variable that takes every value from a start to an end inclusive. A pre-condition loop (WHILE ... ENDWHILE) tests its condition before the body, so it may run zero times, and it continues while the condition is TRUE. A post-condition loop (REPEAT ... UNTIL) tests its condition after the body, so it always runs at least once, and it stops when the condition becomes TRUE.
A statement is nested when it sits entirely inside the block of another statement. The four combinations you must handle are selection inside selection, selection inside iteration, iteration inside selection and iteration inside iteration. The syllabus states that candidates will not be required to write more than three levels of nested statements — so three is the ceiling for anything you produce, and if your answer is heading for four, the right move is to pull the inner part out into a procedure or function.
A two-dimensional array is declared with two pairs of bounds: DECLARE <identifier> : ARRAY[<l1>:<u1>, <l2>:<u2>] OF <data type>. It is usually pictured as a grid of rows and columns. A single element is selected with one index value for each dimension, written in the declared order: NoughtsAndCrosses[2,3]. Visiting every element needs nested iteration — one loop for the first dimension and one for the second — and which loop is on the outside decides whether you travel row by row or column by column.

