Skip to content
Cambridge AS & A Level9618

Constructs

Computer Science 9618 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Constructs notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Constructs notes as text: skim, search, and jump between subtopics.

~16 min read

1. IF, ELSE and nested IF

An IF statement runs its lines only when its condition is TRUE. It is written IF...THEN...ENDIF. An `ELSE` clause holds the lines that run when the condition is FALSE.

``` IF <condition> THEN <statements> ELSE <statements> ENDIF ```

A nested IF is an IF statement inside another one. Each IF needs its own `ENDIF`. Join conditions with AND, `OR` and NOT, not with a comma.

Examiner insight

“Many candidates did not follow the instruction to give four separate IF..THEN...ENDIF statements.” “A number of answers included the use of a comma separator, which was not accepted for pseudocode.”

Worked example 14 marks

Examine the following pseudocode.

``` IF A = TRUE THEN IF B = TRUE THEN IF C = TRUE THEN CALL Sub1() ELSE CALL Sub2() ENDIF ENDIF ELSE IF B = TRUE THEN IF C = TRUE THEN CALL Sub4() ELSE CALL Sub3() ENDIF ELSE IF C = FALSE THEN CALL Sub3() ELSE CALL Sub4() ENDIF ENDIF ENDIF ```

A programmer wants to re-write the pseudocode as four separate IF...THEN...ENDIF statements, each containing a single CALL statement. This involves writing a single, simplified logic expression as the condition in each statement.

Write the amended pseudocode.

  1. 1

    ``` IF A AND B AND C THEN CALL Sub1() ENDIF ``` [1]

  2. 2

    ``` IF (A AND B) AND NOT C THEN CALL Sub2() ENDIF ``` [1]

  3. 3

    ``` IF (NOT A) AND (NOT C) THEN CALL Sub3() ENDIF ``` [1]

  4. 4

    ``` IF (NOT A) AND C THEN CALL Sub4() ENDIF ``` [1]

2. CASE structures

A CASE structure picks one branch by comparing one variable with a list of clauses. It ends with `ENDCASE`.

``` CASE OF <variable> <clause> : <statement> <clause> : <statement> OTHERWISE : <statement> ENDCASE ```

A clause may be a comparison such as `< 30` or a range such as `30 TO 40`. A range includes both of its boundary values. The clauses are checked in sequence, so the first clause that matches is the one used. `OTHERWISE` takes every value that no clause matches.

Examiner insight

“A general point candidates should be aware that a statement such as ‘30 to 40’ does include the two boundary values.” “It was expected that the answer clearly stated, or implied, that the clauses in a case structure are processed in sequence.”

Worked example 13 marks

The following pseudocode represents part of the algorithm for a program:

``` CASE OF ThisValue < 30 : Level ← "Low" // less than 30 Check ← 1 < 20 : Level ← "Very Low" // less than 20 Check ← ThisValue / 2 30 TO 40 : Level ← "Medium" // between 30 and 40 Check ← ThisValue / 3 Data[ThisValue] ← Data[ThisValue] + 1 > 40 : Level ← "High" ENDCASE ```

The pseudocode contains four assignments to variable Level. One of these assignments will never be performed.

Identify this assignment and explain why this is the case.

  1. 1

    The assignment is `Level ← "Very Low"` [1]

  2. 2

    Because the CASE clauses are checked in sequence [1]

  3. 3

    A value less than 20 is also less than 30, so it satisfies the first clause. The clause `< 20` is never tested. [1]

3. Count-controlled loops

A count-controlled loop repeats a block a fixed number of times. It is written FOR...TO...NEXT. The loop variable must be declared. It takes each value from the first to the last, going up by 1 unless `STEP` says otherwise. The loop changes its own variable, so the block must not add 1 to it.

The loop `FOR i ← 3 TO 12 STEP 3` has four iterations, with `i` taking the values 3, 6, 9 and 12.

Use this loop when the number of iterations is known. A counter that is output after the loop must be set to 0 before it.

Examiner insight

“Occasionally a FOR loop incorrectly included an additional statement to increment the loop counter and several loops made an incorrect number of iterations.”

Worked example 15 marks

An algorithm will:

• output each integer value between 100 and 200 that ends with the digit 7, for example, 107 • output a final count of the number of values that are output.

Write pseudocode for this algorithm.

Any variables used must be declared.

  1. 1

    ``` DECLARE ThisInt, Count : INTEGER Count ← 0 ``` [1]

  2. 2

    ``` FOR ThisInt ← 100 TO 200 ``` [1]

  3. 3

    ``` IF ThisInt MOD 10 = 7 THEN ``` [1]

  4. 4

    ``` OUTPUT ThisInt Count ← Count + 1 ENDIF NEXT ThisInt ``` [1]

  5. 5

    ``` OUTPUT Count ``` [1]

4. Pre-condition loops

A pre-condition loop tests its condition before each pass. It is written WHILE...ENDWHILE. The block repeats while the condition is TRUE.

``` WHILE <condition> <statements> ENDWHILE ```

If the condition is FALSE the first time, the block never runs. So the variables in the condition need values before the loop, and the block must change them. A paper's `WHILE Index <= LENGTH(Name)` loop sets `Index ← 1` first and ends the block with `Index ← Index + 1`.

A WHILE loop is a conditional loop: it stops because of a condition, not because a count has run out.

Examiner insight

“Many WHILE loops referenced a ThisNum that had no initial value.”

Worked example 15 marks

A program inputs the number of a month. The number must be in the range 1 to 12 inclusive. The program must keep asking for the number until it is in range, then output it.

Write pseudocode for this algorithm.

Any variables used must be declared.

  1. 1

    Declare the variable: `DECLARE Month : INTEGER` [1]

  2. 2

    Input the first value before the loop: `INPUT Month` [1]

  3. 3

    Loop while the value is out of range: `WHILE Month < 1 OR Month > 12`. The two tests are joined with OR. [1]

  4. 4

    Input again inside the loop, so the value being tested changes: `INPUT Month` [1]

  5. 5

    Close the loop with `ENDWHILE`, then `OUTPUT Month` after it [1]

5. Post-condition loops

A post-condition loop tests its condition after each pass. It is written REPEAT...UNTIL. The block runs at least once and repeats until the condition is TRUE.

``` REPEAT <statements> UNTIL <condition> ```

The loop stops when the condition becomes TRUE. In a paper's loop with `i ← 1` and `UNTIL i = 6`, the block runs for `i` = 1 to 5, so there are 5 iterations.

A post-condition loop that must stop on either of two events joins them with OR: it ends as soon as one of them is TRUE.

When you dry run a loop, write a new row of the trace table for each iteration.

Examiner insight

“It is important when completing trace tables that candidates keep the values of variables distinct for each iteration of the loop.” “Where both conditions were tested, these were usually combined correctly in the case of a pre-condition loop, but often the logical operator which combined the two tests was incorrect in the case of post-condition loops.”

Worked example 16 marks

A global 1D array of integers contains four elements, which are assigned these values:

``` Mix[1] ← 1 Mix[2] ← 3 Mix[3] ← 4 Mix[4] ← 2 ```

A procedure Process() manipulates the values in the array.

The procedure is written in pseudocode:

``` PROCEDURE Process(Start : INTEGER) DECLARE Value, Index, Count : INTEGER Index ← Start Count ← 0 REPEAT Value ← Mix[Index] Mix[Index] ← Mix[Index] - 1 Index ← Value Count ← Count + 1 UNTIL Count = 5 Mix[4] ← Count * Index ENDPROCEDURE ```

Complete the trace table, with columns Index, Value, Count, Mix[1], Mix[2], Mix[3] and Mix[4], by dry running the procedure when it is called as follows:

``` CALL Process(3) ```

  1. 1

    Initialisation row: Index = 3, Count = 0, Mix = 1, 3, 4, 2 [1]

  2. 2

    Count = 1: Value = 4, Index = 4, Mix[3] = 3 [1]

  3. 3

    Count = 2: Value = 2, Index = 2, Mix[4] = 1 [1]

  4. 4

    Count = 3: Value = 3, Index = 3, Mix[2] = 2 [1]

  5. 5

    Count = 4: Value = 3, Index = 3, Mix[3] = 2 [1]

  6. 6

    Count = 5: Value = 2, Index = 2, Mix[3] = 1. The loop ends. Then Mix[4] = 5 × 2 = 10 [1]

6. Justifying the choice of loop

To justify a loop, name it with the syllabus term, then give a reason that comes from the problem.

Count-controlled: the number of iterations is known before the loop starts, such as one pass for each element of an array of 100.

Conditional (pre-condition or post-condition): the number of iterations is not known, because the loop ends following a specific input or event. A post-condition loop suits a block that must run at least once, such as an input that is then checked. A pre-condition loop tests first, so its block may not run at all.

Examiner insight

“Where a count-controlled loop was identified, often the justification was rather convoluted and lacked the clarity of the answer ‘because the number of iterations is known’.” “Many candidates lost a mark for just stating ‘repeat’ loop or ‘while’ loop which was not enough to gain a mark.”

Worked example 12 marks

Study the algorithm:

``` DECLARE Chars : ARRAY[1:4] OF CHAR DECLARE I, J : INTEGER DECLARE Key : CHAR I ← 2 Chars[1] ← 'D' Chars[2] ← 'T' Chars[3] ← 'H' Chars[4] ← 'R' WHILE I <= 4 //Outer loop Key ← Chars[I] J ← I - 1 WHILE J >= 0 AND Chars[J] > Key //Inner loop Chars[J + 1] ← Chars[J] J ← J - 1 ENDWHILE Chars[J + 1] ← Key I ← I + 1 ENDWHILE ```

The outer loop structure used in the algorithm is not the most appropriate one to use.

State the type of loop structure that would be the most appropriate to use and justify why it is the most appropriate.

  1. 1

    Count-controlled loop (`FOR...NEXT`) [1]

  2. 2

    The number of iterations required is known: `I` goes from 2 to 4, which is 3 passes [1]

Recap

  • IF...THEN...ELSE...ENDIF chooses between branches. Each nested IF has its own ENDIF.
  • Join conditions with AND, OR and NOT. A comma is not pseudocode.
  • CASE OF...ENDCASE checks its clauses in sequence. A range such as 30 TO 40 includes both ends. OTHERWISE takes the rest.
  • FOR...NEXT is count-controlled: use it when the number of iterations is known. Initialise any counter before the loop.
  • WHILE...ENDWHILE tests first, so the block may run zero times. Set the tested variable before the loop.
  • REPEAT...UNTIL tests last, so the block runs at least once, and stops when the condition is TRUE.
  • To justify a loop, name it with the syllabus term and give a reason from the problem: the number of iterations is known, or it is not known.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. Give the name of the loop structure that tests its condition after the loop body has run.1 mark
    Answer
    1. Post-condition loop.

    Trap: WHILE tests before the body, so it is a pre-condition loop. REPEAT...UNTIL is the one that tests after.

  2. Study this pseudocode: ``` FOR i ← 2 TO 20 STEP 4 OUTPUT i NEXT i ``` (a) Identify the type of construct used in this pseudocode. (b) State how many iterations are performed by this pseudocode.2 marks
    Answer
    1. (a)
    2. Count-controlled loop.
    3. (b)
    4. `i` takes the values 2, 6, 10, 14 and 18, so there are 5 iterations.

    Trap: (20 - 2) ÷ 4 = 4.5 gives the wrong count. The first value 2 is an iteration too, and the next value, 22, is past 20.

  3. Study this pseudocode: ``` N ← 10 REPEAT OUTPUT N N ← N - 3 UNTIL N < 0 ``` State the values that are output and the final value of N.3 marks
    Answer
    1. The values output are 10, 7, 4 and 1.
    2. After the pass that outputs 1, N = 1 - 3 = -2. The condition `N < 0` is TRUE, so the loop stops.
    3. The final value of N is -2.

    Trap: The output comes before the subtraction, so −2 is never output even though it is the final value of N.

  4. Study this pseudocode: ``` CASE OF Mark < 40 : Grade ← "F" < 60 : Grade ← "C" < 80 : Grade ← "B" OTHERWISE : Grade ← "A" ENDCASE ``` State the value assigned to Grade when Mark is (a) 15, (b) 40 and (c) 80.3 marks
    Answer
    1. (a)
    2. "F": the clauses are checked in sequence and `< 40` matches first.
    3. (b)
    4. "C": 40 is not less than 40, so the first clause fails and `< 60` matches.
    5. (c)
    6. "A": 80 is not less than 80, so no clause matches and OTHERWISE is used.

    Trap: 15 is also less than 60 and 80, but only the first matching clause is used. Choosing "B" for 80 reads `< 80` as including 80.

  5. Study this pseudocode: ``` Total ← 0 Count ← 0 WHILE Total < 50 Count ← Count + 1 Total ← Total + Count * 4 ENDWHILE OUTPUT Count, Total ``` State the values that are output.3 marks
    Answer
    1. Pass 1: Count = 1, Total = 4. Pass 2: Count = 2, Total = 12. Pass 3: Count = 3, Total = 24. Pass 4: Count = 4, Total = 40.
    2. Total is 40, which is less than 50, so the block runs a fifth time: Count = 5, Total = 60.
    3. Now `60 < 50` is FALSE. The output is 5 and 60.

    Trap: Stopping at Total = 40 is wrong: the condition is tested before each pass, and 40 is still below 50.

  6. A program asks the user to enter a password. It repeats the request until the correct password is entered. State the most appropriate loop structure and justify your choice.3 marks
    Answer
    1. Post-condition loop (REPEAT...UNTIL).
    2. The password must be entered at least once before it can be checked.
    3. The number of attempts is not known in advance.

    Trap: Writing ‘repeat loop’ does not gain the mark: use the syllabus term, post-condition loop.

  7. A program inputs whole numbers, one at a time. The value -1 ends the input and is not part of the data. The program outputs the total of the numbers entered before -1. Write pseudocode for this algorithm. Any variables used must be declared.5 marks
    Answer
    1. ``` DECLARE Num, Total : INTEGER Total ← 0 INPUT Num WHILE Num <> -1 Total ← Total + Num INPUT Num ENDWHILE OUTPUT Total ```
    2. Marks: declarations; Total initialised to 0; first INPUT before the loop; WHILE with the condition `Num <> -1` and the addition and second INPUT inside it; OUTPUT after the loop.

    Trap: With one INPUT at the top of the loop, −1 is added to Total. With no INPUT before the loop, Num is tested before it has a value.

  8. A cinema sets the price of a ticket from the age of the customer. • Under 5: the price is 0. • 5 to 15 inclusive: the price is 6. • 16 or over: the price is 10, or 8 if the Boolean variable Student is TRUE. Write pseudocode using nested IF statements to assign the price to Price.5 marks
    Answer
    1. ``` IF Age < 5 THEN Price ← 0 ELSE IF Age <= 15 THEN Price ← 6 ELSE IF Student = TRUE THEN Price ← 8 ELSE Price ← 10 ENDIF ENDIF ENDIF ```
    2. Marks: test for under 5 with price 0; test for 15 or under with price 6; nested test of Student; prices 8 and 10 in the right branches; every IF closed with ENDIF.

    Trap: `Age < 15` in the second test puts a customer aged exactly 15 on the adult price. The boundary 15 belongs to the middle band.

  9. A school has 20 votes to count. Each vote is a single character: 'A', 'B' or 'C'. Any other character is an invalid vote. Write pseudocode that inputs the 20 votes one at a time, using a loop and a CASE structure, and then outputs the number of votes for A, B and C and the number of invalid votes. Any variables used must be declared.6 marks
    Answer
    1. ``` DECLARE Vote : CHAR DECLARE CountA, CountB, CountC, Invalid, Index : INTEGER CountA ← 0 CountB ← 0 CountC ← 0 Invalid ← 0 FOR Index ← 1 TO 20 INPUT Vote CASE OF Vote 'A' : CountA ← CountA + 1 'B' : CountB ← CountB + 1 'C' : CountC ← CountC + 1 OTHERWISE : Invalid ← Invalid + 1 ENDCASE NEXT Index OUTPUT CountA, CountB, CountC, Invalid ```
    2. Marks: declarations and four counters set to 0; FOR loop for 20 iterations; INPUT inside the loop; CASE with the three clauses; OTHERWISE for invalid votes; all four counts output after the loop.

    Trap: A counter never set to 0 starts with an unknown value. Putting the OUTPUT inside the loop prints 20 sets of counts.

  10. A game gives the player at most 5 attempts to guess a number already stored in the integer variable Secret. The program inputs guesses one at a time. It stops as soon as a guess is correct or 5 guesses have been made. It then outputs "Correct" and the number of guesses made, or outputs "Out of attempts". (a) State the most appropriate loop structure and justify your choice. [2] (b) Write pseudocode for the algorithm. Any variables used must be declared. [6]8 marks
    Answer
    1. (a)
    2. Post-condition loop.
    3. The number of iterations is not known: the loop can end after 1 to 5 guesses. At least one guess must be input before it can be checked.
    4. (b)
    5. ``` DECLARE Guess, Attempts : INTEGER Attempts ← 0 REPEAT INPUT Guess Attempts ← Attempts + 1 UNTIL Guess = Secret OR Attempts = 5 IF Guess = Secret THEN OUTPUT "Correct", Attempts ELSE OUTPUT "Out of attempts" ENDIF ```
    6. Marks: declarations and Attempts set to 0; REPEAT with INPUT inside; Attempts increased inside the loop; `UNTIL` with both tests joined by OR; IF after the loop testing `Guess = Secret`; both outputs in the right branches.
    7. Check: a correct fifth guess makes both tests TRUE. The loop ends, `Guess = Secret` is TRUE, and the output is "Correct" and 5.

    Trap: A FOR loop from 1 to 5 cannot stop at a correct guess. Joining the UNTIL tests with AND keeps asking after a correct guess, and testing `Attempts = 5` after the loop reports a correct fifth guess as a failure.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
A loop that will ‘compare with previous numbers’A conditional loop that checks the stored number against values already in the array, and generates another value if it is found
A ‘repeat’ loop or a ‘while’ loopA post-condition loop or a pre-condition loop
`IF A, B, C = TRUE THEN``IF A AND B AND C THEN`
`30 TO 40` has 10 values, as 40 − 30 = 10`30 TO 40` includes both boundary values, so it has 11

Frequently asked questions

What is the difference between a pre-condition and a post-condition loop?
A pre-condition loop, WHILE...ENDWHILE, tests its condition before each pass, so its block may not run at all. A post-condition loop, REPEAT...UNTIL, tests after each pass, so its block runs at least once and stops when the condition is TRUE. Use the syllabus term in an answer, not 'repeat loop' or 'while loop'.
How many times does a count-controlled loop with a STEP run?
Count the values the counter takes, from the first to the last. The loop FOR i ← 3 TO 12 STEP 3 has four iterations, with i taking the values 3, 6, 9 and 12. The loop changes its own variable, so the block must not add 1 to it.
How does a CASE structure choose which branch to run?
A CASE OF statement compares one variable with its clauses in sequence, and the first clause that matches is used. A range such as 30 TO 40 includes both boundary values. OTHERWISE takes every value that no clause matches. A clause placed after a broader one, such as < 20 after < 30, is never tested.
How do I write nested IF statements for several conditions?
Put one IF inside another, and give each IF its own ENDIF. Join conditions with AND, OR and NOT, not commas. Where a question asks for separate statements, write each one as its own IF...THEN...ENDIF, for example IF (A AND B) AND NOT C THEN.
When should I use a count-controlled loop instead of a conditional loop?
Use a count-controlled FOR...NEXT loop when the number of iterations is known before the loop starts, such as one pass for each element of an array of 100. A conditional loop suits the case where the number of iterations is not known, because the loop ends after a specific input or event. Justify it with 'because the number of iterations is known'.
Why does my WHILE loop fail or give the wrong total?
A pre-condition loop tests its condition first, so the variables in the condition must have values before the loop, and the block must change them. Examiners reported WHILE loops that referenced a ThisNum with no initial value. A counter output after the loop must also be initialised before the loop.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters