Loops & Iteration

Loops with Boolean Flags

A Boolean flag is a TRUE/FALSE variable that lets a loop decide when to stop based on a real-world event — such as "a value was found" or "the user typed valid input" — instead of a fixed count. Mastering flags is the key to writing flexible, event-driven loops in CIE pseudocode.

12.1 What are Boolean Flags?

A Boolean flag is a BOOLEAN variable that records whether a particular condition is TRUE or FALSE at any moment during a program's execution. Flags are used inside loops to signal that "something has happened" — for example a value has been found, an error has occurred, or the user has typed valid input — so the loop knows when to stop.

Key idea: A flag does not count anything — it just records state. It is the simplest possible form of memory: "has this happened yet?" answered with TRUE or FALSE.

Declaring and Initialising a Flag

DECLARE Found : BOOLEAN
Found <- FALSE

The flag is declared with the BOOLEAN data type and then initialised to a starting value — usually FALSE for a "Found" or "Valid" flag, because at the start of the loop nothing has been found or validated yet.

Setting the Flag Inside a Loop

DECLARE Found : BOOLEAN
Found <- FALSE
WHILE NOT Found DO
  INPUT Guess
  IF Guess = 7 THEN
    Found <- TRUE
  ENDIF
ENDWHILE
OUTPUT "You guessed it!"

The loop keeps asking for a guess until the user types 7. As soon as the correct guess is made, Found <- TRUE flips the flag, the condition NOT Found becomes FALSE, and the loop ends.

12.2 Using Flags with WHILE

A WHILE loop tests its condition before each iteration. When the condition involves a flag, the loop continues while the flag is in the "not yet" state and stops as soon as the flag is flipped.

Standard Pattern

DECLARE Found : BOOLEAN
Found <- FALSE
WHILE NOT Found DO
  <read or compute something>
  IF <condition is met> THEN
    Found <- TRUE
  ENDIF
ENDWHILE

Reading the condition aloud: "while not Found — that is, while we have not yet found what we are looking for — keep looping." The loop body is responsible for setting Found <- TRUE when the search succeeds.

Worked Example: Guess the Secret Number

DECLARE Secret : INTEGER
DECLARE Guess : INTEGER
DECLARE Found : BOOLEAN
Secret <- 42
Found <- FALSE
WHILE NOT Found DO
  OUTPUT "Enter your guess: "
  INPUT Guess
  IF Guess = Secret THEN
    Found <- TRUE
    OUTPUT "Correct!"
  ELSE
    OUTPUT "Try again."
  ENDIF
ENDWHILE

Each iteration the user types a guess. Only when Guess = Secret does the flag flip to TRUE and the loop terminate. Note that the body might never run at all — if Found were initialised to TRUE the WHILE would skip the body entirely.

Infinite-loop trap: If you forget the line Found <- TRUE inside the loop, the flag stays FALSE forever and the loop never ends. Always make sure the body of the loop has a path that updates the flag.

12.3 Using Flags with REPEAT...UNTIL

A REPEAT...UNTIL loop tests its condition after the body has run. That means the body is guaranteed to execute at least once — perfect for situations where you must do something (like read input) before you can decide whether to continue.

Standard Pattern

DECLARE Valid : BOOLEAN
Valid <- FALSE
REPEAT
  INPUT Age
  IF Age > 0 AND Age < 120 THEN
    Valid <- TRUE
  ELSE
    OUTPUT "Please enter a valid age."
  ENDIF
UNTIL Valid

Reading the UNTIL clause aloud: "repeat the body until Valid becomes TRUE." Notice the polarity is reversed compared to WHILE: WHILE keeps going while a flag is FALSE; REPEAT...UNTIL keeps going until a flag becomes TRUE.

WHILE vs REPEAT...UNTIL with the Same Flag

FeatureWHILE NOT Found DOREPEAT ... UNTIL Found
When is the flag tested?Before the body (pre-test)After the body (post-test)
Body always runs at least once?NoYes
Flag polarity in conditionNOT Found (loop while FALSE)Found (stop when TRUE)
Best forBody may need to be skippedBody must run at least once

Rule of thumb: Use REPEAT...UNTIL for input-validation loops — you always want to ask the user at least once. Use WHILE for searches where the data set might be empty and the body should be skipped entirely.

12.4 Common Flag Patterns

Experienced programmers reuse a small set of flag patterns again and again. Learning to recognise them will help you read and write CIE pseudocode much faster.

Found flag

Search — set TRUE when the target value is located.

Found <- FALSE
WHILE NOT Found DO ...

Valid flag

Input validation — set TRUE when input passes the checks.

Valid <- FALSE
REPEAT ... UNTIL Valid

Done flag

Process completion — set TRUE when a long process finishes.

Done <- FALSE
WHILE NOT Done DO ...

Error flag

Error handling — set TRUE when something goes wrong.

Error <- FALSE
IF ... THEN Error <- TRUE

Combining Multiple Flags

A loop can be controlled by several flags at once, joined with the logical operators AND, OR and NOT.

DECLARE Found : BOOLEAN
DECLARE TimedOut : BOOLEAN
Found <- FALSE
TimedOut <- FALSE
WHILE NOT Found AND NOT TimedOut DO
  <read next item>
  IF <item matches target> THEN
    Found <- TRUE
  ENDIF
  IF <timer exceeded> THEN
    TimedOut <- TRUE
  ENDIF
ENDWHILE
IF Found THEN
  OUTPUT "Found it!"
ELSE
  OUTPUT "Timed out."
ENDIF

The loop ends as soon as either flag flips to TRUE — the search succeeds OR the timer expires, whichever happens first. After the loop, an IF on the Found flag tells you which case occurred.

Naming tip: Always choose names that read as a TRUE/FALSE statement — Found, Valid, GameOver, HasError. Avoid vague names like X or Flag1 that tell the reader nothing about the condition being tracked.

12.5 Practical Examples

Here are three worked examples that show Boolean flags solving real exam-style problems.

Example 1 — Searching an Array

Find whether the value 42 appears anywhere in an array of 10 integers.

DECLARE Numbers : ARRAY[1:10] OF INTEGER
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN
// ... assume Numbers is filled with values ...
Found <- FALSE
Index <- 1
WHILE NOT Found AND Index <= 10 DO
  IF Numbers[Index] = 42 THEN
    Found <- TRUE
  ELSE
    Index <- Index + 1
  ENDIF
ENDWHILE
IF Found THEN
  OUTPUT "42 found at position ", Index
ELSE
  OUTPUT "42 not in the array."
ENDIF

Two conditions control the loop: the Found flag (success) and the Index counter (end of array). Whichever becomes TRUE first stops the loop. The final IF uses the flag to print the right message.

Example 2 — Input Validation (Retry Until Valid)

Keep asking the user for an exam mark in the range 0–100 until they enter a valid value.

DECLARE Mark : INTEGER
DECLARE Valid : BOOLEAN
Valid <- FALSE
REPEAT
  OUTPUT "Enter a mark between 0 and 100: "
  INPUT Mark
  IF Mark >= 0 AND Mark <= 100 THEN
    Valid <- TRUE
  ELSE
    OUTPUT "Invalid mark — try again."
  ENDIF
UNTIL Valid
OUTPUT "Accepted: ", Mark

Because the body must run at least once (we have to ask the user at least one time), REPEAT...UNTIL is the natural choice. The Valid flag starts FALSE and is set TRUE only when the mark is in range.

Example 3 — Simple Game Loop

A turn-based game that keeps running until the player chooses to quit.

DECLARE GameOver : BOOLEAN
DECLARE Choice : STRING
GameOver <- FALSE
WHILE NOT GameOver DO
  OUTPUT "Play / Quit?"
  INPUT Choice
  IF Choice = "Quit" THEN
    GameOver <- TRUE
  ELSE
    // ... play one turn of the game ...
    OUTPUT "You played a turn."
  ENDIF
ENDWHILE
OUTPUT "Thanks for playing!"

The GameOver flag is the heart of any game loop. While it stays FALSE, the game keeps playing turns. The moment the player types "Quit", the flag flips and the loop exits cleanly.

12.6 Key Points Summary

Recap the most important ideas about Boolean flags before you tackle the Question Bank.

A Boolean flag is a BOOLEAN variable used to record TRUE/FALSE state.
Declare with: DECLARE <name> : BOOLEAN.
Initialise the flag BEFORE the loop (usually to FALSE).
WHILE NOT Flag DO ... ENDWHILE — loop while Flag is FALSE.
REPEAT ... UNTIL Flag — loop until Flag becomes TRUE (runs at least once).
The loop body MUST contain a path that updates the flag, otherwise the loop is infinite.
Combine multiple flags with AND / OR / NOT in the loop condition.
After the loop, use an IF on the flag to decide what to output.
Choose meaningful flag names: Found, Valid, Done, GameOver, HasError.
REPEAT is best for input validation; WHILE is best for searches that may find nothing.

Exam tip: When tracing a flag-controlled loop in an exam, draw a small column for each flag and write its value after every iteration. This makes it obvious exactly when the flag flips and the loop terminates.

Question Bank

Read each question, think of your answer, then click Reveal Answer.

12 questions

Question 1Structured

Define the term "Boolean flag" as used in CIE pseudocode and give one reason programmers use flags inside loops.

Question 2Multiple Choice

Which line correctly declares a Boolean flag named Valid in CIE pseudocode?

ADECLARE Valid : BOOL
BDECLARE Valid : BOOLEAN
CBOOLEAN Valid
DDECLARE BOOLEAN Valid

Question 3True / False

A Boolean flag in CIE pseudocode can hold any of the values TRUE, FALSE, or UNKNOWN.

ATRUE
BFALSE

Question 4Multiple Choice

DECLARE Found : BOOLEAN
Found <- FALSE
WHILE NOT Found DO
  OUTPUT "Searching..."
ENDWHILE

Look at this pseudocode. What is the main issue with it?

AThe flag is initialised incorrectly
BThe flag is never set to TRUE inside the loop, so the loop will run forever
CThe WHILE condition should be "WHILE Found DO"
DFound should be an INTEGER, not a BOOLEAN

Question 5Structured

Explain the difference between using a Boolean flag with a WHILE loop versus a REPEAT...UNTIL loop, and give one situation where each is preferable.

Question 6Fill in the Blank

Complete the REPEAT...UNTIL header so the loop stops when Found becomes TRUE: REPEAT ... UNTIL _____

Question 7True / False

In a WHILE loop controlled by a flag, the loop condition is usually written as "WHILE NOT Flag DO".

ATRUE
BFALSE

Question 8Multiple Choice

DECLARE Target : INTEGER
DECLARE Guess : INTEGER
DECLARE Found : BOOLEAN
Found <- FALSE
Target <- 7
WHILE NOT Found DO
  INPUT Guess
  IF Guess = Target THEN
    Found <- TRUE
  ENDIF
ENDWHILE
OUTPUT "Correct!"

Look at this pseudocode. What is OUTPUT if the user enters 7?

ANothing — the loop never ends
B"Correct!" is output once after the user types 7
C"Correct!" is output every time the user types a number
DAn error because Found is not initialised

Question 9Structured

Why is it important to initialise a Boolean flag BEFORE entering the loop that uses it?

Question 10Multiple Choice

Which of the following is NOT a typical use of a Boolean flag?

ARecording whether a value has been found in a search
BRecording whether user input passed validation
CStoring the running total of a series of numbers
DRecording whether a process has finished

Question 11True / False

An infinite loop occurs if a flag-controlled loop never updates the flag inside its body.

ATRUE
BFALSE

Question 12Multiple Choice

DECLARE Valid : BOOLEAN
DECLARE Age : INTEGER
Valid <- FALSE
WHILE Valid = TRUE DO
  INPUT Age
  IF Age > 0 AND Age < 120 THEN
    Valid <- TRUE
  ENDIF
ENDWHILE
OUTPUT "Accepted"

Look at this pseudocode. Identify the bug.

AThe flag is initialised incorrectly — should start TRUE
BThe WHILE condition should be "WHILE NOT Valid DO" so the loop runs while input is invalid
CThe IF condition should use OR instead of AND
DThere is no bug — the code works correctly