Sequence (Cambridge (CIE) O Level Computer Science)
Revision Note
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 |
|
02 |
|
03 |
|
04 |
|
05 |
|
06 |
|
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 |
---|
""" 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
# ------------------------------------------------------------------------# Main program # ------------------------------------------------------------------------
|
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 assignedThe correct sequence is:
# Calculate areaarea = length * width
return area
Last updated:
You've read 0 of your 5 free revision notes this week
Sign up now. It’s free!
Did this page help you?