2 - Practical Problem Solving & Programming / Sequence
|
|
Sequence
Sequence is one of the three main programming structures - Sequence, Selection, Iteration. Sequence is perhaps the simplest of these three concepts to understand.
Sequence
Sequence refers to code or sections of code that run line by line, in sequential order without deviation.
Look at the example below. This code will run each line in order with no deviation - line 1 then line 2 then line 3...
Look at the example below. This code will run each line in order with no deviation - line 1 then line 2 then line 3...
Sequence example
When coding sections of code that run in sequence like this it is very important that they are written in the correct order. Look at the example below and take note that line 1 and 2 have been flipped from the previous example.
Sequence order example
Making this change to the code will result in the user having to enter some data as soon as the program starts. Unfortunately, they will have no idea what they are supposed to enter as the prompt does not appear until after they have entered something.
It is possible to identify sequence based code within other structures e.g. selection or iteration. Any section of code that has to run line by line is classed as sequence even if it is just 2 or three lines within an if statement etc...
Look at the code below, which lines here do you think would be classed as sequence?
Look at the code below, which lines here do you think would be classed as sequence?
Sequence within other structures
The lines that would be classed as sequence are:
Lines 1-2
Lines 4 - 10
Line 12
Lines 3, 11 and 13 your be classed as selection
Lines 1-2
Lines 4 - 10
Line 12
Lines 3, 11 and 13 your be classed as selection
Vertical Divider