# Control flow structures

> Computer Science · CIE A-Level 9618
> Source: https://www.owlsprep.com/study/cie-9618-u11-control-flow-structures/

This module covers the three core categories of control flow structures for procedural programming: sequence, selection, and iteration. We explain syntax, common use cases, and how to avoid logical errors in nested structures for CIE 9618 exams.

**Prerequisites:** [Basic programming syntax and variables](https://www.owlsprep.com/study/cie-9618-u11-basic-programming-concepts/); [Boolean logic and expressions](https://www.owlsprep.com/study/cie-9618-u10-boolean-logic/)

## Learning objectives

- Classify the three core types of control flow structures in procedural programming
- Write syntactically and logically correct code for sequence, selection and iteration
- Identify and fix common errors in nested control structures
- Match the correct loop/selection type to a given problem requirement

## 1. Sequential Control Flow

Sequential execution is the default behavior of all procedural programming languages. Unless explicitly modified by another control structure, statements run one after another in the exact order they are written in source code.

**Sequential Structure** — A linear control flow structure where each statement executes exactly once, in the order it appears in the source code.

*Example:* Reading user input, calculating a result, then outputting the result

**Worked example:** Write a sequential pseudocode block that calculates the area of a circle given radius $r$.

1. 1. Declare and read the input radius value:
2. `INPUT r`
3. 2. Calculate the area using the formula $area = \pi r^2$:
4. `area = 3.14159 * r * r`
5. 3. Output the final calculated area:
6. `OUTPUT area`

> **Exam tip:** Always check that you never use a variable before it is assigned a value — this is the most common error in sequential code.

## 2. Selection (Conditional) Control Flow

Selection structures alter the default sequence by executing different blocks of code based on whether a boolean condition evaluates to true or false. CIE 9618 accepts four common forms: single-branch `IF`, double-branch `IF-ELSE`, multiple-branch `IF-ELSE IF-ELSE`, and `SWITCH-CASE`.

**Selection Structure** — A control flow structure that selects between two or more mutually exclusive code blocks to execute, based on the result of one or more boolean conditions.

**Worked example:** Write a selection block that outputs a grade 'A' for marks ≥ 80, 'B' for 70-79, and 'F' for marks below 70.

1. Start with the outer IF for the highest grade band:
2. `IF mark >= 80 THEN
  OUTPUT "A"`
3. Add the ELSE IF for the next grade band:
4. `ELSE IF mark >= 70 THEN
  OUTPUT "B"`
5. Add the final ELSE for the failing grade and close:
6. `ELSE
  OUTPUT "F"
ENDIF`

**Check your understanding**

What is the output for `mark = 70` in the code above?

1. What is the output?

   - A
   - B
   - F
   - Syntax error

   *Why:* Conditions are checked in order: the first condition `mark >= 80` fails, so the second condition `mark >= 70` is checked and passes, outputting B.

## 3. Iteration (Loop) Control Flow

Iteration structures (loops) repeat a block of code multiple times, as long as a continuation condition is met. CIE 9618 distinguishes three core loop types with different use cases.

| Loop Type | When condition is checked | Minimum iterations | Common use case |
| --- | --- | --- | --- |
| Count-controlled (FOR) | Before each iteration | 0 | Iterate over a fixed range |
| Pre-condition (WHILE) | Before each iteration | 0 | Validate user input |
| Post-condition (REPEAT-UNTIL) | After each iteration | 1 | Approximate calculation to tolerance |

**Worked example:** Write a WHILE loop that calculates the sum of all even numbers from 2 to 10.

1. Initialize sum and counter variables before the loop starts:
2. `total = 0
current = 2`
3. Define the WHILE loop continuation condition:
4. `WHILE current <= 10 DO`
5. Update the total and increment the counter inside the loop:
6. `total = total + current
current = current + 2`
7. Close the loop and output the result:
8. `ENDWHILE
OUTPUT total`

> **tip**
>
> Always update your loop exit condition inside the loop. Forgetting this step causes an infinite loop, which loses significant marks in CIE exams.

## 4. Nested Control Structures

Any control flow structure can be nested inside another, for example a loop inside an IF statement, or an IF inside another IF. Nested structures follow the same rules as top-level structures: the inner structure only executes if the outer structure's path is selected.

> **Indentation Best Practice**
>
> Always indent inner nested blocks by one consistent level. This makes it much easier to match opening keywords (IF, WHILE) to their corresponding closing keywords (ENDIF, ENDWHILE).

**Worked example:** Write a nested control block that outputs all prime numbers between 2 and 20.

1. 1. Outer loop to iterate through each candidate number 2 to 20:
2. `FOR candidate FROM 2 TO 20 DO`
3. 2. Nested selection to check if the candidate is prime:
4. `IF isPrime(candidate) = TRUE THEN
  OUTPUT candidate
ENDIF`
5. 3. Close the outer for loop:
6. `ENDFOR`

## Common pitfalls

- **Wrong:** Forgetting to add a closing keyword for nested control structures (e.g. missing ENDIF/ENDWHILE)
  - Why it fails: Unclosed structures cause syntax errors and unexpected behavior in the entire program
  - Correct: Write the closing keyword immediately after writing the opening keyword, then add the inner code between them. Use indentation to match pairs.
- **Wrong:** Using a REPEAT-UNTIL loop when a WHILE loop is required (or vice versa)
  - Why it fails: REPEAT-UNTIL always runs at least once, while WHILE can run zero times, changing output for edge cases
  - Correct: Check if the code block must run at least once: if yes, use REPEAT-UNTIL, otherwise use WHILE.
- **Wrong:** Forgetting to update the loop counter inside a WHILE loop
  - Why it fails: This leaves the exit condition always true, creating an infinite loop
  - Correct: Initialize the counter before the loop, and update it as the last step inside the loop body.
- **Wrong:** Using the wrong comparison operator in an IF condition (e.g. = instead of >=)
  - Why it fails: Off-by-one errors on boundary conditions are extremely common and cost easy marks
  - Correct: Double-check all boundary values (e.g. the pass mark of 40) to confirm your condition matches the question requirement.
- **Wrong:** Reversing the order of steps in sequential code
  - Why it fails: Using a variable before it is assigned causes runtime errors or incorrect results
  - Correct: Map the order of operations on paper before writing code, to ensure all values are defined before use.

## Cheatsheet

| Control Flow Type | Key Feature | Common Use Case |
| --- | --- | --- |
| Sequence | Default line-by-line execution | Simple input-process-output workflows |
| Single IF | One optional code path | Trigger an action if a condition is true |
| IF-ELSE | Two mutually exclusive paths | Binary pass/fail or yes/no checks |
| FOR Loop | Fixed number of iterations | Iterate over a known range of values |
| WHILE Loop | Pre-check condition, variable iterations | Validate user input until valid |
| REPEAT-UNTIL | Post-check condition, at least 1 iteration | Approximate calculation to tolerance |

## What's next

Control flow is the foundation of all procedural programming you will encounter in CIE 9618. Mastering these core structures now makes it much easier to debug logical errors in more complex programs later in your course. Next, you can learn how to encapsulate reusable control flow blocks in subroutines, and use nested loops to process multi-dimensional data structures like 2D arrays, a common exam question topic.

- [Procedures, Functions and Parameters](https://www.owlsprep.com/study/cie-9618-u11-procedures-functions-and-parameters/)
- [File Handling](https://www.owlsprep.com/study/cie-9618-u11-file-handling/)
- [Object-oriented programming concepts](https://www.owlsprep.com/study/cie-9618-u11-object-oriented-programming-concepts/)

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/cie-9618-u11-control-flow-structures/
