Pre-Condition Loops (WHILE ... DO ... ENDWHILE)

A Pre-Condition Loop (the WHILE loop) evaluates its entry condition before executing the body of the loop.


1. Syntax of WHILE ... DO ... ENDWHILE

WHILE <Condition> DO
    // Statements executed while Condition remains TRUE
ENDWHILE

Critical Operational Characteristics:

  1. Zero Iterations Possible: If <Condition> is initially FALSE, the loop body is bypassed entirely and runs zero times.
  2. Continuation Condition: The loop continues executing as long as the condition evaluates to TRUE. It terminates the instant the condition becomes FALSE.
  3. Updating the Condition: Statements inside the loop body must modify at least one variable present in <Condition>, otherwise the loop will never terminate, resulting in an infinite loop.

2. Choosing Between the Three Loop Types

Question to Ask

Appropriate Loop Construct

Cambridge Syntax

Is the exact number of iterations known in advance?

Count-Controlled Loop

FOR i <- 1 TO N ... NEXT i

Must the loop execute at least once (e.g. user input)?

Post-Condition Loop

REPEAT ... UNTIL <Condition>

May the loop need to execute zero times (e.g. searching)?

Pre-Condition Loop

WHILE <Condition> DO ... ENDWHILE

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

Need guidance? Ask a question or use the hint below.

AI Socratic Tutor is an All-Access Benefit

Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.

You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.

Exam-Style Check: Pre-Condition Loop Execution Guarantee

• Multiple Choice
+25 XP
Question

If the condition of a WHILE ... DO ... ENDWHILE loop evaluates to FALSE before the loop begins, how many times will the loop body execute?

All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.