1.61 Operations Manual

Use this page for instructions on how sequence, selection and repetition are implemented in pseudocode and Python, with clear examples you can follow when stuck.

Control structures define the flow of logic in a program. They allow the computer to process instructions in a specific order, make decisions, or repeat tasks. Regardless of complexity, all algorithms rely on just three structures: sequence, selection, and repetition. Each can be used on its own or combined to model complex behaviour.

Sequence

A sequence is a set of instructions executed one after the other. This is the default control structure and doesn’t involve any decision-making or repetition.

INPUT username
OUTPUT "Welcome, " + username

Selection

Selection allows a program to choose between paths based on a condition. In Python, this is done with if, elif, and else.

Binary selection

Binary selection refers to a decision-making process where the program chooses between two possible paths based on a specified condition, typically using constructs like if and else.

IF age >= 18 THEN
    OUTPUT "You can vote."
ELSE
    OUTPUT "You are too young to vote."
ENDIF

Multiway selection

Multiway selection allows a program to choose from multiple paths based on different conditions. In Python, this is achieved using if, elif, and else statements. Each elif provides an additional condition to evaluate, and the else serves as a catch-all for cases where none of the preceding conditions are met. This structure enables more complex decision-making processes when multiple criteria must be considered.

BEGIN
    CASEWHERE
        grade >= 90 : OUTPUT "A"
        grade >= 80 : OUTPUT "B"
        grade >= 70 : OUTPUT "C"
        grade < 70 : "Needs improvement"
    ENDCASE
END

Nested selection

Nested selection involves placing an if statement inside another if, elif, or else statement. This allows for more intricate decision-making processes by evaluating additional conditions only if the previous conditions are met. It provides a way to handle complex scenarios where decisions depend on multiple layers of conditions being satisfied.

IF has_account THEN
    IF password_correct THEN
        OUTPUT "Access granted."
    ELSE
        OUTPUT "Wrong password."
    ENDIF
ELSE
    OUTPUT "No account found."
ENDIF

Repetition

Repetition allows a program to execute a block of code multiple times. Python supports several looping patterns.

Pre-test loop (WHILE)

A pre-test loop, such as the while loop in Python, evaluates the loop condition before executing the loop's body. If the condition is true, the loop's code block runs; if false, the loop terminates. This makes it useful for scenarios where the number of iterations depends on a condition evaluated at each start of the loop.

SET count TO 0
WHILE count < 5
    OUTPUT "Count is", count
    SET count TO count + 1
ENDWHILE

Post-test loop (REPEAT/UNTIL)

A post-test loop evaluates the loop condition after executing the code block. It ensures that the loop's body runs at least once before checking the condition, making it suitable for situations where the operations must occur at least once, regardless of the condition.

Python doesn’t support repeat-until directly, but we simulate it using while True and break.

REPEAT
    INPUT password
UNTIL password = "admin"

Counted loop (FOR)

A counted loop, often implemented using a for loop, is a loop that iterates a specific number of times. It is ideal for situations where the number of iterations is known beforehand. In Python, a for loop is commonly used with the range() function to control the number of times the loop executes.

FOR i = 1 TO 5
    OUTPUT "Round", i
NEXT i

Combining structures

Control structures can be combined in any order. A loop might contain a selection; a selection might depend on the outcome of a previous loop.

FOR i = 0 TO 2 STEP 1
    INPUT name
    IF name = "" THEN
        OUTPUT "Blank name entered. Skipping..."
        CONTINUE
    ENDIF
    OUTPUT "Hello", name
NEXT i

Key concepts

  • Sequence executes instructions in order.

  • Selection uses conditions to choose a path (if, elif, else).

  • Repetition runs instructions multiple times (while, for, repeat-until).

  • Control structures can be nested or combined.

  • break exits a loop early; continue skips the rest of the current iteration.

Last updated

Was this helpful?