Back to Topics/Loops & Iteration/WHILE Loop
Page 11.xLoops & Iteration

WHILE Loop

Master the pre-condition loop that tests first and may run zero times. Learn the CIE syntax (WHILE...DO...ENDWHILE), trace tables, how to choose between WHILE, REPEAT, and FOR, and apply WHILE to sentinel reading, bounded sums, and boolean-flag-controlled loops.

6
Sections
30
KC Items
12
Questions
Loops
Category

11.1 What is a WHILE Loop?

A WHILE loop is a pre-condition loop — the condition is tested first, and only if it is TRUE does the body execute. This means the body may run zero or more times: if the condition is FALSE at the very first check, the body is skipped entirely.

  • 1
    Pre-condition: The condition is evaluated before each pass through the body, not after.
  • 2
    May run zero times: If the condition is initially FALSE, the body is never executed — unlike REPEAT...UNTIL.
  • 3
    Continue condition: The loop continues while the condition is TRUE and stops when it becomes FALSE (opposite of REPEAT...UNTIL).
  • 4
    Manual update required: The loop control variable must be updated inside the body, otherwise the loop runs forever.

Key Idea

Think of WHILE as "as long as this is true, keep doing this." The check happens at the start of each iteration — if it fails immediately, the loop never begins.

Common Use

WHILE is ideal for sentinel-controlled reading (e.g. read values until a special stop value) and state-driven loops where the loop should not run if the initial state already satisfies the exit condition.

11.2 Syntax

The CIE pseudocode syntax for a WHILE loop uses three keywords:WHILEto start (with the condition), DOto mark the start of the body, andENDWHILEto close the loop.

General Form
WHILE <condition> DO
  <statements to repeat>
ENDWHILE
Example: Count from 1 to 5
DECLARE Counter : INTEGER
Counter <- 1
WHILE Counter <= 5 DO
  OUTPUT Counter
  Counter <- Counter + 1
ENDWHILE

WHILE

Starts the loop. The condition follows on the same line.

DO

Marks the beginning of the loop body. Comes after the condition.

ENDWHILE

Closes the loop. No condition is written here — control jumps back to WHILE.

Watch Out

Every WHILE must have a matching ENDWHILE. And the DO keyword is required in CIE pseudocode — forgetting it is a common syntax error. Indent the body to make the structure clear.

The condition can be any expression that produces a BOOLEAN result, such as Counter <= 5, Value <> -1, or (Total < Limit) AND (More = TRUE).

11.3 Tracing

A trace table for a WHILE loop records the value of the loop control variable and the condition before each iteration. If the condition is FALSE at the start, the body is skipped and no rows are added.

Example to Trace
DECLARE Counter : INTEGER
DECLARE Total : INTEGER
Counter <- 1
Total <- 0
WHILE Counter <= 4 DO
  Total <- Total + Counter
  Counter <- Counter + 1
ENDWHILE
OUTPUT Total

Trace table:

IterationCounter (start)Counter <= 4?Total (after)Counter (after)
11TRUE — run body12
22TRUE — run body33
33TRUE — run body64
44TRUE — run body105
5FALSE — exit loop

The loop exits when Counter becomes 5, because 5 <= 4 is FALSE. The final value of Total is 10(1 + 2 + 3 + 4), which is what the OUTPUT prints.

Tracing Rule

In a WHILE loop, the condition is tested before each iteration. If it starts FALSE, the body is skipped — record "FALSE — exit" as the first (and only) row.

Subtle Case — Body Skipped Entirely
DECLARE X : INTEGER
X <- 100
WHILE X < 10 DO
  OUTPUT "Inside loop"
  X <- X + 1
ENDWHILE
OUTPUT "Done"

Here X starts at 100. The condition X < 10 is FALSE on the very first check, so "Inside loop" is neverprinted. The program jumps straight to printing "Done".

11.4 WHILE vs REPEAT vs FOR

CIE pseudocode has three loop constructs. Choosing the right one is a common exam skill. The decision rests on two questions: do you know how many times to iterate in advance? and must the body run at least once?

FeatureFORWHILEREPEAT...UNTIL
Iteration count known in advance?Yes (definite)No (indefinite)No (indefinite)
Condition checkedAuto (counter)Before bodyAfter body
Body runs at least onceYes (if range valid)No (may skip)Yes (always)
Loop continues when condition isN/ATRUEFALSE
Loop exits when condition isN/AFALSETRUE
Closing keywordNEXTENDWHILEUNTIL (with condition)
Best forCounted iterationSentinel/state loopsInput validation, menus
Same Logic — Three Different Loops (print 1 to 5)
// FOR — count is known
FOR Counter <- 1 TO 5
  OUTPUT Counter
NEXT Counter

// WHILE — pre-condition
DECLARE Counter : INTEGER
Counter <- 1
WHILE Counter <= 5 DO
  OUTPUT Counter
  Counter <- Counter + 1
ENDWHILE

// REPEAT...UNTIL — post-condition
DECLARE Counter : INTEGER
Counter <- 1
REPEAT
  OUTPUT Counter
  Counter <- Counter + 1
UNTIL Counter > 5

Decision Guide

  • Know the count?FOR
  • May not run at all?WHILE
  • Must run at least once?REPEAT...UNTIL

Exam Tip

A question that says "read numbers until -1 is entered" could use either WHILE or REPEAT — but if the very first input might be -1 and the body should not process it, WHILE is safer. A question that says "the user must be prompted at least once" signals REPEAT...UNTIL.

11.5 Practical Examples

WHILE is most often used for sentinel-controlled reading, state-driven loops, and bounded accumulation. Let's look at a worked example of each.

Example 1 — Sentinel-Controlled Reading

Read numbers until the user enters 0 (excluded from sum)
DECLARE Value : INTEGER
DECLARE Total : INTEGER
Total <- 0
OUTPUT "Enter a value (0 to stop): "
INPUT Value
WHILE Value <> 0 DO
  Total <- Total + Value
  OUTPUT "Enter a value (0 to stop): "
  INPUT Value
ENDWHILE
OUTPUT "Total is ", Total

The first INPUT is read before the WHILE so the condition has something to test. Inside the body, each new INPUT updates Value. When the user enters 0, the loop exits — and 0 is not added to Total.

Example 2 — Bounded Accumulation

Add 1 + 2 + 3 + ... while the total stays under 100
DECLARE N : INTEGER
DECLARE Sum : INTEGER
N <- 1
Sum <- 0
WHILE Sum + N < 100 DO
  Sum <- Sum + N
  N <- N + 1
ENDWHILE
OUTPUT "Final sum is ", Sum
OUTPUT "Stopped at N = ", N

Here the loop checks whether adding the next value would stay under 100. If not, it exits before the addition — Sum never overshoots.

Example 3 — State-Driven Loop

Keep playing until the player chooses to quit
DECLARE KeepGoing : BOOLEAN
DECLARE Choice : STRING
KeepGoing <- TRUE
WHILE KeepGoing = TRUE DO
  OUTPUT "Playing a turn..."
  OUTPUT "Type QUIT to stop, anything else to continue: "
  INPUT Choice
  IF Choice = "QUIT" THEN
    KeepGoing <- FALSE
  ENDIF
ENDWHILE
OUTPUT "Game over"

A boolean flag (KeepGoing) controls the loop. The flag is initialised to TRUE before the loop, and flipped to FALSE inside the body when the user types QUIT.

Initialise First

Because WHILE tests the condition first, you must initialise the loop control variable before the loop. Forgetting to do this leaves the variable undefined and the condition unpredictable.

11.6 Key Points Summary

Here is a quick recap of everything we've covered on WHILE loops:

  • Pre-condition loopThe condition is checked BEFORE the body executes.
  • May run zero timesIf the condition is initially FALSE, the body is skipped entirely.
  • Continue on TRUEThe loop keeps iterating while the condition is TRUE; it stops when FALSE.
  • Three keywordsWHILE <condition> DO ... ENDWHILE — all three are required in CIE pseudocode.
  • Initialise firstThe loop control variable must be given a value BEFORE the WHILE statement.
  • Manual updateYou must update the loop control variable inside the body to avoid infinite loops.
  • Choose by intentUse FOR for known counts, WHILE for sentinel/state, REPEAT for at-least-once prompts.

Exam Tip

When a question says "while X is true, keep doing Y" or "keep reading until the sentinel appears (but the sentinel might be first)" — reach for WHILE. Always initialise the control variable before the loop, and remember to update it inside the body.

Question Bank

Test your understanding — 12 questions on WHILE loops

Q1Multiple Choice

When is the condition evaluated in a WHILE loop?

Q2Multiple Choice

How many times can a WHILE loop body execute?

Q3True / False

A WHILE loop stops when its condition becomes FALSE.

Q4Multiple Choice

Look at this code. What is the final OUTPUT? DECLARE C : INTEGER C <- 0 WHILE C < 10 DO C <- C + 3 ENDWHILE OUTPUT C

Q5True / False

If the WHILE condition is FALSE on the very first check, the body is skipped entirely.

Q6Multiple Choice

Which is the correct CIE pseudocode syntax for a WHILE loop?

Q7Multiple Choice

You need to read input values until the user enters 0 — but 0 might be the first value. Which loop is BEST?

Q8True / False

The loop control variable must be initialised BEFORE the WHILE statement.

Q9Multiple Choice

Trace this loop. How many times does the body execute? DECLARE X : INTEGER X <- 10 WHILE X > 0 DO X <- X - 2 ENDWHILE

Q10True / False

WHILE is the EXIT condition — the loop stops when it becomes TRUE.

Q11Multiple Choice

Look at this code. What is OUTPUT? DECLARE N : INTEGER N <- 50 WHILE N < 10 DO OUTPUT "Inside" N <- N + 1 ENDWHILE OUTPUT "Done"

Q12Multiple Choice

Which loop ALWAYS runs its body at least once, regardless of the initial condition?

Answered 0 of 12 questions