Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
OCR GCSE Computer Science (Paper 2) Smart practice A mixed round aimed at your weak spots
Unit 1
Algorithms
Unit 2
Programming fundamentals
Unit 3
Producing robust programs
Unit 4
Boolean logic
Unit 5
Programming languages and IDEs
Unit 6
Practical programming (Section B)
Avg
score:
–
Topics
done:
0/31

Iteration: count-controlled and condition-controlled loops

Practise 33 exam-style questions on Iteration: count-controlled and condition-controlled loops for OCR GCSE 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 the Study Helper on hand if you get stuck.

  • 33 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

2 marks

State the output when this code runs.

for i = 10 to 0 step -3
    print(i)
next i

Model answer

10
7
4
1

Explanation

step -3 makes i count down by 3 each pass, starting at 10. The loop continues while i has not gone past the end value 0, so it runs for 10, 7, 4 and 1. The next value would be -2, which is past 0, so the loop stops; 0 itself is never reached because it is not in the sequence.

Mark scheme
  1. 1 mark

    10, 7, 4 (in this order)

    Correct output with no working scores both marks. Accept on one line or on separate lines.

  2. 1 mark

    1 is the last value output (and nothing after it: no 0 or -2)

    Do not award if 0 or -2 is also output.

Question 2

2 marks

This program should output the total number of steps walked in a week.

01for day = 1 to 7
02    total = 0
03    steps = int(input("Enter steps for the day"))
04    total = total + steps
05next day
06print(total)

The program only outputs the number of steps entered for the last day.

Explain why, and state how to correct the program.

Model answer

Line 02 is inside the loop, so total is reset to 0 at the start of every day, and only the last day's steps are left in it at the end. total = 0 should be moved to before the for loop (above line 01).

Explanation

A running total must be initialised once, before the loop starts. Anything inside the loop runs on every pass, so here each day's steps wipe out the previous days. With total = 0 moved above the loop, each pass adds to the total built up so far.

Mark scheme
  1. 1 mark

    total is set back to 0 on every pass of the loop (line 02 is inside the loop)

    Accept 'total is reset each day'.

  2. 1 mark

    move total = 0 (line 02) to before the loop / before line 01

Question 3

3 marks

Write a program that outputs the numbers from 10 down to 1, one on each line, and then outputs "Lift off!".

You must use a count-controlled loop. You must use OCR Exam Reference Language or a high-level programming language.

Model answer

for i = 10 to 1 step -1
    print(i)
next i
print("Lift off!")

Explanation

A negative step makes the loop count down, and the end value 1 is included in OCR Exam Reference Language. The final message must come after next i, so it is output once rather than ten times. Without step -1, for i = 10 to 1 would not run at all, because the start is already past the end.

Mark scheme
  1. 1 mark

    a for loop from 10 to 1

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors. Python: range(10, 0, -1); range(10, 1, -1) loses this mark (misses 1).

  2. 1 mark

    counts down: step -1

    Accept for i = 1 to 10 with print(11 - i): this correct alternative scores this mark and the previous one.

  3. 1 mark

    outputs the loop variable inside the loop and "Lift off!" once, after the loop

    Do not award if "Lift off!" is inside the loop.

Question 4

5 marks

A word game has the secret word mango.

Write a program that keeps asking the user to guess the secret word until they enter mango, and then outputs the number of guesses they made.

You must use OCR Exam Reference Language or a high-level programming language.

Model answer

guesses = 0
do
    guess = input("Guess the secret word")
    guesses = guesses + 1
until guess == "mango"
print("You took " + str(guesses) + " guesses")

Explanation

The number of guesses is not known in advance, so a condition-controlled loop is needed. do ... until fits well, because the user must guess at least once. The counter is set to 0 before the loop and increased on every pass, including the final correct guess, and the result is output once the loop has ended. With a while loop, guess must be given a starting value first, or the condition cannot be tested.

Mark scheme
  1. 1 mark

    sets a counter to 0 before the loop

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors. Accept starting the counter at 1 if the first guess is input (and counted) before the loop and the final count is still correct.

  2. 1 mark

    a condition-controlled loop that stops when the guess is "mango"

    e.g. do ... until guess == "mango" / while guess != "mango". A while loop needs guess to have a value before the loop (e.g. guess = "" or a first input).

  3. 1 mark

    inputs a guess inside the loop

  4. 1 mark

    adds 1 to the counter for every guess

    The correct guess must also be counted.

  5. 1 mark

    outputs the number of guesses after the loop

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

Study Helper Hints first, answers after you try

Stuck on this one? Ask for a hint and I’ll point you in the right direction, without giving the answer away. Once you’ve checked your answer, I’ll show you how the marks are awarded.