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 <- FALSEThe 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
ENDWHILEReading 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
ENDWHILEEach 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 ValidReading 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
| Feature | WHILE NOT Found DO | REPEAT ... UNTIL Found |
|---|---|---|
| When is the flag tested? | Before the body (pre-test) | After the body (post-test) |
| Body always runs at least once? | No | Yes |
| Flag polarity in condition | NOT Found (loop while FALSE) | Found (stop when TRUE) |
| Best for | Body may need to be skipped | Body 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 ValidDone 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 <- TRUECombining 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."
ENDIFThe 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."
ENDIFTwo 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: ", MarkBecause 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.
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.