Three Types of Control Structure
Control structures govern the flow of execution of instructions in a program, that is, which instructions run and when. There are three basic types: sequence, selection and repetition. Every program, no matter how complex, is built only from a combination of these three structures, and combining them lets a program solve almost any kind of problem.
Sequence
Sequence is the execution of instructions one after another from top to bottom, without skipping or repeating any instruction. It is the most basic structure and forms the backbone of the other two. For example, reading a name, then reading an age, then displaying a message is a three-step sequence.
Selection
Selection lets a program make a decision based on a condition. The if...then...else structure runs one block of instructions when the condition is true and another block when it is false. A condition always evaluates to a boolean value (true or false). This lets a program react differently to different situations, for example displaying "Pass" or "Fail" based on a student's mark.
Example
IF mark >= 50 THEN PRINT "Pass" ELSE PRINT "Fail" END IF
If the mark is 70, the program prints "Pass".
Repetition
Repetition (a loop) repeats a block of instructions several times. There are two common kinds:
- for loop — used when the number of repetitions is known, for example repeating 10 times.
- while loop — repeats as long as a condition is true; the number of repetitions may not be known in advance.
Key idea
Sequence = instructions in order; selection = choose a path based on a condition; repetition = repeat instructions while/until a condition is met.
Remember
Make sure every loop has a stopping condition that will be reached, otherwise it becomes an infinite loop that never ends.