Menu

Variables & Constants

Program Structure and Command Sequences

A program is built from structural components: the pieces a programmer fits together to make a working solution. They are constants and variables (which hold values), initialisation and assignment statements (which store those values), command sequences, selection, repetition and iteration, data structures (which hold groups of values), subprograms and parameters (which make reusable blocks of code), and input and output.

The simplest component is the command sequence: a series of statements carried out one after another, in exactly the order they are written. The computer runs the first statement in full, then the second, then the third, and so on until it reaches the end of the program. Nothing in the sequence is skipped and nothing is repeated.

Order matters

Statements in a sequence usually depend on each other, so where you place each one changes what the program does. In the program below, questions_right and points_per_question must be given their values before score can be calculated, and score must exist before it can be printed.

# Points scored in a quiz round
questions_right = 14
points_per_question = 3
bonus_points = 10
score = questions_right * points_per_question + bonus_points
print("Final score: " + str(score))

Output:

Final score: 52

If you moved the line that works out score to the top of the program, it would fail, because questions_right would not have a value at that point. The statements would still run in order — but the wrong order gives a program that cannot work.

A command sequence on its own always follows the same path, because every statement runs once. To make a program skip a statement or repeat one, you need the other components, such as selection and repetition, alongside the sequence.

Practise this