Sequence, selection and iteration

GCSE Computer Science (OCR) · OCR J277 · Algorithms and programming (Paper 2) › Programming fundamentals › Programming fundamentals

66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 11 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.

Revision notes

Every program is built from three constructs: sequence, selection and iteration. You need to recognise them in code, know the difference between count-controlled and condition-controlled loops, and use if, switch, for, while and do … until in your own code.

Key facts

  • Sequence: instructions run in order, one after another
  • Selection: if … then … elseif … else … endif, or switch … case … default … endswitch
  • Count-controlled: for i = 1 to 10 … next i (a fixed number of times)
  • for … step 2 counts in 2s; step -1 counts down; the end value is included
  • while … endwhile: condition checked at the start (may run 0 times)
  • do … until: condition checked at the end (runs at least once)
  • Nesting: one construct inside another

Notes

Sequence and selection

diagram
  • Sequence: instructions are carried out one after another, in the order they are written.
  • Selection: a condition decides which instructions run next.
    if mark >= 70 then
       print("Merit")
    elseif mark >= 50 then
       print("Pass")
    else
       print("Fail")
    endif
  • Only the first true condition runs, so the order of the conditions matters.
  • mark >= 70?YesOutput "Merit"Nomark >= 50?YesOutput "Pass"NoOutput "Fail"
    Selection as a flowchart: only the first true condition's branch runs
  • A switch (case) statement selects based on one variable's value:
    switch choice:
       case "P":
          print("Play")
       case "Q":
          print("Quit")
       default:
          print("Unknown option")
    endswitch

Iteration (loops)

diagram
  • Count-controlled: repeats a set number of times. for i = 1 to 5 … next i runs 5 times. for i = 10 to 0 step -2 counts down in 2s: 10, 8, 6, 4, 2, 0.
  • Condition-controlled: repeats until a condition changes, so the number of repeats is not known in advance.
  • while … endwhile checks the condition at the start, so the code inside may never run.
  • do … until checks the condition at the end, so the code inside always runs at least once.
    do
       pin = input("Enter a 4-digit PIN")
    until pin.length == 4
  • while: check at the startdo … until: check at the endconditiontrue?Yesloop bodyNoexitloop bodyconditiontrue?NoYesexit
    while checks before the loop body; do … until checks after it

Nesting

  • Constructs can be put inside each other, for example an if statement inside a loop. This is called nesting.
  • Indent each level so it is clear which lines belong to which loop or if statement.

How to answer each type of question

Identify the constructs in code

1 to 3 marksGrade 4
  1. Selection: look for if or switch.
  2. Count-controlled iteration: for … next.
  3. Condition-controlled iteration: while or do … until.
  4. Give the line number(s) the question asks for.

Example. Look at the code.

01 total = 0
02 for day = 1 to 7
03    steps = int(input("Steps today?"))
04    total = total + steps
05 next day
06 if total >= 70000 then
07    print("Target met")
08 endif
(a) Identify the line where selection starts. [1]
(b) State the type of iteration used and explain how you know. [2]

Show the model answerHide the model answer
(a) Line 06
(1)
(b) Count-controlled iteration (1): it is a for loop that runs a fixed number of times, 7
(1).

State the output of a loop

1 to 2 marksGrade 5
  1. Write down the value of the loop variable each time round.
  2. For a for loop, remember the end value is included if it is reached exactly.
  3. For a while loop, check the condition before every pass.

Example. State the output of this code.

for i = 1 to 10 step 3
   print(i)
next i
[2]

Show the model answerHide the model answer
1, 4, 7 (1) and 10 (1).

Compare types of iteration

2 marksGrade 5
  1. Count-controlled: repeats a fixed, known number of times.
  2. Condition-controlled: repeats until (or while) a condition is met, so the number of repeats is not known in advance.

Example. Describe the difference between count-controlled and condition-controlled iteration. [2]

Show the model answerHide the model answer
Count-controlled iteration repeats a set number of times (1). Condition-controlled iteration repeats until a condition is met, so the number of repetitions does not need to be known in advance (1).

Write code with a condition-controlled loop

3 marksGrade 7
  1. Pick do … until if the code must run at least once (for example asking for input).
  2. Put the input inside the loop.
  3. Write the condition for when the loop should stop.

Example. Write an algorithm that keeps asking the user to enter a whole number from 1 to 10 until a valid number is entered. [3]

Show the model answerHide the model answer
do
   num = int(input("Enter a number from 1 to 10"))
until num >= 1 AND num <= 10
Condition-controlled loop (1); input inside the loop (1); correct condition for 1 to 10 inclusive (1).

Don’t lose marks

  • Putting conditions in the wrong order, such as checking mark >= 50 before mark >= 70, so a Merit is never given.
  • Forgetting that a for loop includes its end value.
  • Writing a while loop whose condition never becomes false (an infinite loop), for example forgetting to input a new value inside the loop.
  • Saying a while loop always runs at least once: that is do … until.
  • Missing endif, endwhile or next, or indenting so that a line is in the wrong loop.

More tips

Memory tricks

  • SSI: Sequence = in order, Selection = choose, Iteration = repeat.
  • while checks first ('look before you leap'); do … until checks last ('leap, then look').
  • In an if … elseif chain, put the most demanding condition first (for example 70 before 50).

Exam technique

  • When a loop must repeat a known number of times, use a for loop; examiners look for the right type of loop.
  • Close every construct (endif, next i, endwhile, until) so your structure is clear.
  • Trace a loop for its first two and its last time round before writing the output.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 3
    Name the three programming constructsSequence (in order), selection (decisions) and iteration (repetition).
  2. Grade 4
    Identify each construct in given codePoint to the lines that use if or switch, and the lines that use a loop.
  3. Grade 5
    Compare count- and condition-controlled loopsA for loop repeats a set number of times; while and do … until repeat depending on a condition.
  4. Grade 5
    Work out the output of a loopFollow for loops with step values and while loops, including the last time round.
  5. Grade 6
    Write code using if, elseif and elseChoose the right order of conditions so that each case gives the correct result.
  6. Grade 7
    Choose and write the most suitable loopUse for when the number of repeats is known and do … until when it must run at least once.
  7. Grade 8
    Use nested selection and iterationPut loops and if statements inside each other, with correct indentation and logic.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

Look at this program.
01 total = 0
02 for i = 1 to 5
03    num = int(input("Enter a number"))
04    total = total + num
05 next i
06 if total > 100 then
07    print("Large")
08 else
09    print("Small")
10 endif
Identify the line number where selection starts.
06
Name the type of loop that repeats a set number of times.
Count-controlled loop (e.g. a for loop)
State the purpose of the else part of an if statement.
It holds the instructions that run when the condition is false.
In the OCR Exam Reference Language, write the symbol used to test whether two values are equal.
==
An if statement has no else part and its condition is false. State what the program does next.
It skips the if block and continues after endif.

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy4 marks
Look at this program.
01 total = 0
02 for i = 1 to 5
03    num = int(input("Enter a number"))
04    total = total + num
05 next i
06 if total > 100 then
07    print("Large")
08 else
09    print("Small")
10 endif
(a) Identify the line number where selection starts.
[1]
(b) Identify the line number where iteration starts.
[1]
(c) State the name of the programming construct in which instructions are carried out one after another, in order.
[1]
(d) State how many numbers the user is asked to enter.
[1]
Show the answer and mark scheme
(a) Answer: 06
  • 06 / line 6
(b) Answer: 02
  • 02 / line 2
(c) Answer: Sequence
  • sequence
(d) Answer: 5
  • 5
Question 2Medium5 marks
Look at this algorithm.
01 for k = 3 to 15 step 4
02    print(k)
03 next k
(a) State the output of the algorithm.
[1]
(b) Rewrite the algorithm using a while loop so that it produces the same output.
[3]
(c) Line 01 is changed to for k = 3 to 14 step 4. State how many times the loop now runs.
[1]
Show the answer and mark scheme
(a) Answer: 3, 7, 11, 15
  • 3, 7, 11, 15
(b) Answer:
k = 3
while k <= 15
   print(k)
   k = k + 4
endwhile
  • sets k to 3 before the loop
  • loop condition k <= 15 (or k < 16 / k < 19)
  • outputs k and then adds 4 to k inside the loop
(c) Answer: 3 (k = 3, 7, 11)
  • 3
Question 3Hard8 marks
A cinema charges for tickets as follows.
  • On Tuesdays every ticket costs £6.00, whatever the age of the customer.
  • On other days, customers under 12 pay £5.50, customers aged 12 to 17 pay £7.00 and customers aged 18 or over pay £10.50.
(a) Write a program that asks the user to enter the day and the customer’s age, and outputs the price of the ticket.
You must use either OCR Exam Reference Language or a high-level programming language that you have studied.
[6]
(b) State four ages that should be tested on a Wednesday to check the boundaries of the age bands.
[2]
Show the answer and mark scheme
(a) Answer:
day = input("Enter the day")
age = int(input("Enter the age"))
if day == "Tuesday" then
   price = 6.00
elseif age < 12 then
   price = 5.50
elseif age <= 17 then
   price = 7.00
else
   price = 10.50
endif
print(price)
  • inputs the day and the age (age as an integer)
  • checks for Tuesday before (or separately from) the age checks
  • gives 6.00 on a Tuesday
  • gives 5.50 when the age is less than 12
  • gives 7.00 for ages 12 to 17 inclusive
  • gives 10.50 for ages 18 and over (and outputs the price)
(b)
  • 11 and 12
  • 17 and 18

More in Programming fundamentals

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.