Why does order matter in a program?
A computer follows instructions from top to bottom. It does not rearrange them or guess what should happen first.
Sequence is a programming construct where instructions run once, in the order they are written.
Sequence means running instructions once, in the exact order they are written. Change the order, and you may change the result.
A computer follows instructions from top to bottom. It does not rearrange them or guess what should happen first.
Sequence is a programming construct where instructions run once, in the order they are written.
In a sequence, there are no choices and no repeated instructions. The program simply moves from one statement to the next.
Input, calculations, assignment and output can all be arranged as one sequence.
Imagine a product moving along an assembly line. First one part is fitted, then another, and finally the product is checked.
If the checking stage happened before the product was built, the process would make no sense. A program is the same: each instruction must happen at the correct stage.
The program must first collect the age, then calculate the new value, and only then display it.
OUTPUT("Enter your age")
INPUT Age
AgeNextYear ← Age + 1
OUTPUT("Next year you will be ", AgeNextYear)
If the user enters 14, the program calculates 14 + 1.
When defining sequence, include both parts of the definition.
Do not only say that the instructions happen “one after another”; make clear that each statement runs once.
The computer does not rearrange the program. It follows the statements exactly as written.