Sequence (Cambridge (CIE) O Level Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Sequence

What is sequence?

  • Sequence refers to lines of code which are run one line at a time

  • The lines of code are run in the order that they written from the first line of code to the last line of code

  • Sequence is crucial to the flow of a program, any instructions out of sequence can lead to unexpected behaviour or errors

Example 1

  • A simple program to ask a user to input two numbers, number two is subtracted from number one and the result is outputted

Line

Pseudocode

01

OUTPUT "Enter the first number"

02

INPUT Num1

03

OUTPUT "Enter the second number"

04

INPUT Num2

05

Result ← Num1 - Num2

06

OUTPUT Result

  • A simple swap of line 01 and line 02 would lead to an unexpected behaviour, the user would be prompted to input information without knowing what they should enter

Example 2

Python example

def calculate_area(length, width):

"""

This function calculates the area of a rectangle

Inputs:

length: The length of the rectangle

width: The width of the rectangle

Returns:

The area of the rectangle

"""

# Calculate area

return area

area = length * width

# ------------------------------------------------------------------------# Main program

# ------------------------------------------------------------------------

length = 5

width = 3

correct_area = calculate_area(length, width)

print(f"Correct area (length * width): {correct_area}")

  • In the example, the sequence of instructions is wrong and would cause a runtime error

  • In the calculate_area() function a value is returned before it is assigned

  • The correct sequence is:

# Calculate area
area = length * width
return area

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.