Study Guide

Control flow structures

Computer ScienceΒ· 30 min read

1. 1. Sequential Control Flowβ˜…β˜†β˜†β˜†β˜†β± 5 min

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.

πŸ“˜ Definition

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 .

  1. 1
    1. Declare and read the input radius value:
  2. 2

    INPUT r

  3. 3
    1. Calculate the area using the formula :
  4. 4

    area = 3.14159 * r * r

  5. 5
    1. Output the final calculated area:
  6. 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. 2. Selection (Conditional) Control Flowβ˜…β˜…β˜†β˜†β˜†β± 10 min

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.

πŸ“˜ Definition

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. 1

    Start with the outer IF for the highest grade band:

  2. 2

    IF mark >= 80 THEN OUTPUT "A"

  3. 3

    Add the ELSE IF for the next grade band:

  4. 4

    ELSE IF mark >= 70 THEN OUTPUT "B"

  5. 5

    Add the final ELSE for the failing grade and close:

  6. 6

    ELSE OUTPUT "F" ENDIF

βœ“ Quick check

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

  1. What is the output?

    • A

    • B

    • F

    • Syntax error

    Reveal answer
    B β€”

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

3. 3. Iteration (Loop) Control Flowβ˜…β˜…β˜…β˜†β˜†β± 15 min

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. 1

    Initialize sum and counter variables before the loop starts:

  2. 2

    total = 0 current = 2

  3. 3

    Define the WHILE loop continuation condition:

  4. 4

    WHILE current <= 10 DO

  5. 5

    Update the total and increment the counter inside the loop:

  6. 6

    total = total + current current = current + 2

  7. 7

    Close the loop and output the result:

  8. 8

    ENDWHILE OUTPUT total

4. 4. Nested Control Structuresβ˜…β˜…β˜…β˜…β˜†β± 10 min

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.

πŸ“ Worked Example

Write a nested control block that outputs all prime numbers between 2 and 20.

  1. 1
    1. Outer loop to iterate through each candidate number 2 to 20:
  2. 2

    FOR candidate FROM 2 TO 20 DO

  3. 3
    1. Nested selection to check if the candidate is prime:
  4. 4

    IF isPrime(candidate) = TRUE THEN OUTPUT candidate ENDIF

  5. 5
    1. Close the outer for loop:
  6. 6

    ENDFOR

5. Common Pitfalls

Wrong move:

Forgetting to add a closing keyword for nested control structures (e.g. missing ENDIF/ENDWHILE)

Why:

Unclosed structures cause syntax errors and unexpected behavior in the entire program

Correct move:

Write the closing keyword immediately after writing the opening keyword, then add the inner code between them. Use indentation to match pairs.

Wrong move:

Using a REPEAT-UNTIL loop when a WHILE loop is required (or vice versa)

Why:

REPEAT-UNTIL always runs at least once, while WHILE can run zero times, changing output for edge cases

Correct move:

Check if the code block must run at least once: if yes, use REPEAT-UNTIL, otherwise use WHILE.

Wrong move:

Forgetting to update the loop counter inside a WHILE loop

Why:

This leaves the exit condition always true, creating an infinite loop

Correct move:

Initialize the counter before the loop, and update it as the last step inside the loop body.

Wrong move:

Using the wrong comparison operator in an IF condition (e.g. = instead of >=)

Why:

Off-by-one errors on boundary conditions are extremely common and cost easy marks

Correct move:

Double-check all boundary values (e.g. the pass mark of 40) to confirm your condition matches the question requirement.

Wrong move:

Reversing the order of steps in sequential code

Why:

Using a variable before it is assigned causes runtime errors or incorrect results

Correct move:

Map the order of operations on paper before writing code, to ensure all values are defined before use.

6. Quick Reference 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

When this came up on past exams

AI-estimated based on syllabus patterns β€” cross-check with official past papers for accuracy. Use only as revision-focus signals.

  • 2022 Β· 12

    Nested selection logic question

  • 2023 Β· 11

    Loop type comparison question

  • 2024 Β· 22

    Control flow refactoring task

Going deeper

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.