Three control structures
A control structure decides the order in which the instructions of a program run. There are three basic types: sequence, selection and repetition. Every algorithm can be built from these three, so understanding them lets you read and write clear programs. Most real programs mix all three together to do useful work.
Sequence and selection
Sequence means the steps run one after another, in the order they are written. For example: boil water, add a tea bag, then pour, where each step follows the one before it. If the order is wrong, the result will be wrong even when every single step is correct.
Selection chooses between different paths using a condition. It usually uses IF … THEN … ELSE. The program tests a condition and then follows only one branch; the other branch is skipped.
Example
IF mark >= 50 THENOUTPUT "Pass"ELSE OUTPUT "Fail"
Repetition
Repetition, also called a loop, repeats a block of steps many times. This saves you from writing the same instructions again and again, so the program stays short and is easier to change. A loop may repeat a fixed number of times, or keep going until a condition is met.
Example
REPEAT 5 TIMESOUTPUT "*"
This prints a star five times.
Remember
- Sequence = steps in order.
- Selection = choose a path using a condition (IF).
- Repetition = repeat steps using a loop.