Control Flow in Imperative Languages

KS3 Computer Science

11-14 Years Old

48 modules covering EVERY Computer Science topic needed for KS3 level.

GCSE Computer Science

14-16 Years Old

45 modules covering EVERY Computer Science topic needed for GCSE level.

A-Level Computer Science

16-18 Years Old

66 modules covering EVERY Computer Science topic needed for A-Level.

GCSE High and Low Level Languages (14-16 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

A-Level Procedural and Object-oriented Languages (16-18 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

Candidates should be able to:

understand and use sequence in an algorithm
understand and use selection in an algorithm (IF and CASE statements)
understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops).


What is sequence in an algorithm?

Program instructions in sequence

Sequence is a control structure where the computer executes the instructions in the order they are written. Each instruction cannot be executed until the previous instruction is completed.

Example: This pseudocode algorithm uses the sequential instructions INPUT and OUTPUT and will simply execute the instructions in sequence, starting at the top.

INPUT item_price
INPUT vat_rate
vat_amount = item_price * vat_rate
final_price = item_price + vat_amount
OUTPUT final_price
END

How are selection and iteration used in an algorithm?

Conditional control structure

Selection and iteration are examples of instructions that do not have to be executed in sequence and are used to control the order that instructions are executed in.

Selection operations

These allow the program to execute instructions based on the outcome of a selection.

  • IF – only if the conditions of the IF statement are TRUE then the instructions following the statement be executed and any furhter ELSEIF or ELSE conditions are ignored.
  • ELSEIF (optional) – if the conditions of the IF statement are not true then a series of further checks can be made using ELSEIF. If any of the ELSEIF statements are TRUE then the instructions following the ELSEIF statement are executed and any further ELSEIF or ELSE conditions are ignored.
  • ELSE (optional) – if non of the IF or ELSEIF conditions are TRUE then the outcome is FALSE and the instructions following the ELSE statement will be executed. If an ELSE is not used then the IF instructions are just bypassed and there are no alternative instructions.
  • ENDIF – this terminates the IF/ELSEIF/ELSE selection.
  • CASE – This is used with CASE OF and ENDCASE to select one instruction from a set of instructions, depending on the value of a variable.

Conditional example 1 – This algorithm uses the conditional control operations IF / THEN / ELSE / ENDIF.

INPUT hoursWorked
IF hoursWorked >= 35 THEN
payRate = 2
ELSEIF hours_worked >= 25 THEN
payRate = 1.5
ELSE
payRate = 1
ENDIF
gross_pay = hoursWorked * payRate
OUTPUT grossPay
END

Conditional example 2 – This algorithm uses the conditional control operations CASE OF / CASE / ENDCASE.

INPUT hours_worked
CASE OF hours_worked
CASE >= 35 : pay_rate = 2
CASE >= 25 : pay_rate = 1.5
CASE : pay_rate = 1
ENDCASE
gross_pay = hoursWorked * payRate
OUTPUT grossPay
END

Iterative operations

These repeat a set of instructions in a continuous loop until a set condition is met.

  • WHILE – this is used with ENDWHILE to repeat a set of instructions until a condition is TRUE. The check is carried out at the start of each loop therefore the instructions in the loop may never be carried out if the condition is FALSE at the beginning.
    REPEAT – this is used with UNTIL to repeat a set of instructions until a condition is FALSE. The check is carried out at the end of each loop therefore the instructions in the loop will be be carried out at least once.
    FOR – this uses a counter and is used with TO and NEXT to repeat a set of instructions a fixed number of times.

Iterative example 1 – This algorithm uses the iterative control operation WHILE / ENDWHILE to carry out a block of sequential instructions until a condition is met. The check is carried out at the start of each cycle.

GET total_items
SET total_cost = 0
SET count = 0
WHILE count < total_items
GET item_price
total_cost = total_cost + item_price
count = count + 1
ENDWHILE
average_price = total_cost / total_items
DISPLAY average
END

Iterative example 2 – This algorithm uses the iterative control operation REPEAT / UNTIL to carry out a block of sequential instructions until a condition is met. The check is carried out at the end of each cycle.

GET total_items
SET total_cost = 0
SET count = 0
REPEAT
GET item_price
total_cost = total_cost + item_price
count = count + 1
UNTIL count = total_items
average_price = total_cost / total_items
DISPLAY average
END

Iterative example 3 – This algorithm uses the iterative control operation FOR / TO / ENDFOR to carry out a block of sequential instructions a set number of times.

GET total_items
SET total_cost = 0
FOR count = 1 TO total_items
GET item_price
total_cost = total_cost + item_price
ENDFOR
average_price = total_cost / total_items
DISPLAY average
END

Further Readings: