Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

Iteration: FOR, REPEAT and WHILE loops

Practise 36 exam-style questions on Iteration: FOR, REPEAT and WHILE loops for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 36 questions
  • 1–7 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

1 mark

Write a pseudocode statement that adds the value stored in the variable Cost to a running total stored in the variable Bill.

Model answer

Bill ← Bill + Cost

Explanation

Totalling adds the new value to the existing total and stores the result back in the same variable. The assignment arrow ← is used in Cambridge pseudocode, never =, which is only used to compare values.

Mark scheme
  1. 1 mark

    Bill ← Bill + Cost

    Accept Bill ← Cost + Bill. Do not accept = instead of ←, or Bill + Cost on its own.

Question 2

2 marks

The flowchart shows an algorithm that inputs the times of the 8 laps of a race and outputs the total time.

State what should be written in decision A and in box B.

Flowchart, read from the top. START, then the process box "Total ← 0", then the process box "Lap ← 1", then decision box A (if Yes, the input/output box "INPUT Time", then the process box "Total ← Total + Time", then process box B, then the flow line goes back up into decision box A; if No, the loop ends). After the loop: the input/output box "OUTPUT Total", then STOP.

Model answer

A: Lap ≤ 8?
B: Lap ← Lap + 1

Explanation

This is a count-controlled loop drawn out in full: the counter starts at 1, the decision checks whether all 8 laps have been processed, and the counter is increased by 1 at the end of each pass. If the counter is not increased, the loop never ends.

Mark scheme
  1. 1 mark

    A: Lap ≤ 8?

    Accept Lap <= 8 or Lap < 9.

  2. 1 mark

    B: Lap ← Lap + 1

    Do not accept Lap + 1 on its own.

Question 3

3 marks

Write pseudocode using a FOR loop to output the 7 times table from 1 × 7 to 12 × 7.

Each line of output must be in the form: 3 x 7 = 21

Model answer

FOR Multiplier ← 1 TO 12
    OUTPUT Multiplier, " x 7 = ", Multiplier * 7
NEXT Multiplier

Explanation

The loop counter does two jobs: it controls the 12 repetitions and it is also used in the calculation. OUTPUT can join several values separated by commas, so the counter, the text and the result appear on one line.

Mark scheme
  1. 1 mark

    MP1 FOR loop from 1 to 12, closed with NEXT and the same variable

  2. 1 mark

    MP2 calculates loop variable × 7 inside the loop

    e.g. Multiplier * 7

  3. 1 mark

    MP3 outputs the number, " x 7 = " and the result on one line, inside the loop

    Ignore minor spacing differences in the output.

Question 4

5 marks

A courier inputs the weight, in kg, of each parcel loaded onto a van. The value 0 is entered when there are no more parcels; it might be the first value entered.

Write pseudocode using a pre-condition loop to:
- count the parcels and total their weights
- after the loop, output the number of parcels and the total weight, or the message "No parcels" if no parcels were entered.

Model answer

Count ← 0
TotalWeight ← 0.0
INPUT Weight
WHILE Weight <> 0 DO
    Count ← Count + 1
    TotalWeight ← TotalWeight + Weight
    INPUT Weight
ENDWHILE
IF Count = 0
  THEN
    OUTPUT "No parcels"
  ELSE
    OUTPUT Count, " parcels, total weight ", TotalWeight, " kg"
ENDIF

Explanation

Inputting the first value before the loop and the next value at the end of the body means the rogue value is tested before it can be processed. If 0 is entered first, the loop body never runs and Count stays at 0, which the IF after the loop detects.

Mark scheme
  1. 1 mark

    MP1 counter and total both set to 0 before the loop

    max 5

  2. 1 mark

    MP2 first weight input before the loop

  3. 1 mark

    MP3 WHILE Weight <> 0 DO ... ENDWHILE

    Accept Weight > 0.

  4. 1 mark

    MP4 adds 1 to the counter and adds the weight to the total inside the loop

  5. 1 mark

    MP5 next weight input at the end of the loop body

  6. 1 mark

    MP6 after the loop, outputs "No parcels" if the count is 0, otherwise outputs the count and the total

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.