Unable to open this exercise
Common exam pitfall
Students often claim WHILE loops always execute at least once, confusing them with REPEAT UNTIL loops.
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:
- Zero Iterations Possible: If
<Condition>is initiallyFALSE, the loop body is bypassed entirely and runs zero times. - Continuation Condition: The loop continues executing as long as the condition evaluates to
TRUE. It terminates the instant the condition becomesFALSE. - 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 |
|
Must the loop execute at least once (e.g. user input)? | Post-Condition Loop |
|
May the loop need to execute zero times (e.g. searching)? | Pre-Condition Loop |
|
AI Socratic Tutor
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 ChoiceIf the condition of a WHILE ... DO ... ENDWHILE loop evaluates to FALSE before the loop begins, how many times will the loop body execute?
Interactive exercises for Module 2 and beyond require an active plan.