6.4 - Program Flow: Sequence, Selection & Iteration
Introduction to program flow
Program flow describes the order in which instructions in a program are executed.
Three main features of program flow
- Sequence - Instructions are followed one after another in a straight line, with no choices or repeats.
- Selection - Decisions are made based on conditions, which determine which path the program takes next.
- Iteration - Sections of code are repeated a certain number of times or until a condition is met.
These features can be represented in flow diagrams, where rectangles show processes, diamonds indicate decisions, and arrows show the direction of flow.
Sequence in programs
Sequence is the simplest form of program flow. It involves executing each instruction exactly once, in the order they appear, without any branches or loops. This creates a linear path through the program.
How sequence works
- Each line of code is processed from top to bottom.
- There are no conditions to check or sections to repeat.
- This structure is ideal for straightforward tasks, such as basic calculations.
Example of sequence in pseudocode
Here is an example of a program that takes two numbers, calculates their total and difference, and prints the results.
How this sequence works:
- The program starts by collecting inputs.
- It then performs the additions and subtractions.
- Finally, it outputs the results by converting numbers to strings for concatenation.
- Every instruction runs exactly once, in order.
In a flow diagram, a sequence appears as a series of rectangular boxes connected by arrows pointing downwards, showing a single path with no branches.
Selection in programs
Selection allows programs to make decisions based on conditions. It uses structures like IF statements to choose between different paths, ensuring the code responds dynamically to inputs or situations.
Basic IF-THEN-ELSE structure
An IF statement checks a condition and executes different code depending on whether it is true or false.
How IF-THEN-ELSE works:
- The condition is evaluated first.
- If true, the code after THEN runs.
- If false, the code after ELSE runs.
- The statement ends with ENDIF.
Example of IF-THEN-ELSE in pseudocode:
This program checks a score and prints a pass or fail message:
In a flow diagram, this appears as a diamond decision box with two arrows: one labelled "Yes" (true) leading to one path, and one labelled "No" (false) leading to another.
IF-ELSEIF statements for multiple conditions
IF-ELSEIF structures check additional conditions only if previous ones are false. This allows handling several possibilities in sequence.
How IF-ELSEIF works:
- The first IF is always checked.
- ELSEIF is checked only if needed.
- A final ELSE handles cases where all conditions are false.
Example of IF-ELSEIF in pseudocode:
This extends the score check to recognise a perfect score:
Nested IF statements
Nested IF statements place one IF inside another. The inner IF is only checked if the outer condition is true.
How nested IF statements work:
- This creates layers of decisions.
- Not all IF statements need an ELSE.
Example of nested IF in pseudocode:
This checks for a pass and then for a perfect score:
In flow diagrams, nested decisions appear as diamonds within the path of another diamond's true branch.
Iteration in programs
Iteration repeats a section of code multiple times, which saves writing the same instructions repeatedly. There are two main types: count-controlled (repeats a fixed number of times) and condition-controlled (repeats based on a condition).
Count-controlled loops using FOR
A FOR loop repeats code a specific number of times, controlled by a counter variable.
How FOR loops work:
- It starts at an initial value and ends at a final value.
- The counter can be a number or a variable.
- Use FOR loops when the number of repetitions is known in advance.
Example of a FOR loop in pseudocode:
This adds numbers from 1 to 7:
The loop runs seven times, with k taking values 1 through 7. In a flow diagram, it would show a loop arrow pointing back after the process.
Nested FOR loops
Nested FOR loops place one FOR inside another. The inner loop completes all its repetitions for each step of the outer loop. This is useful for tasks like generating grids or patterns.
Example of nested FOR loops in pseudocode:
This prints pairs of numbers:
Output:
1, 1
1, 2
2, 1
2, 2
3, 1
3, 2
The inner loop runs two times for each of the outer loop's three iterations.
Condition-controlled loops
These loops repeat based on a condition, not a fixed count. The number of repetitions is unknown in advance.
WHILE loops
A WHILE loop checks the condition at the start and repeats while it is true.
How WHILE loops work:
- If the condition is false initially, the loop never runs.
- It can loop forever if the condition stays true.
Example of a WHILE loop in pseudocode:
This tracks sales until a target is met:
In a flow diagram, the decision diamond is at the start, with a "Yes" arrow looping back through the process.
DO UNTIL loops:
A DO UNTIL loop checks the condition at the end and repeats until it is true.
How DO UNTIL loops work:
- It always runs at least once.
- It can loop forever if the condition never becomes true.
Example of a DO UNTIL loop in pseudocode:
This is an alternative for the sales target:
In a flow diagram, the process comes first, followed by the decision diamond, with a "No" arrow looping back.
Comparing WHILE and DO UNTIL
Both can solve similar problems, but their behaviour differs if the condition is met immediately.
Key differences:
- For a target of 0, WHILE skips the loop, but DO UNTIL runs once.
- Choose based on whether the code must run at least once.