Unable to open this exercise
Common exam pitfall
Students often forget DO in the WHILE header or close with ENDLOOP instead of 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:
- 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 Question: Complete the Pre-Condition WHILE Loop
Complete the missing keywords in this Cambridge pre-condition loop that halves a number until it is less than or equal to 1:
[Space 1] Value > 1 [Space 2]
Value <- Value / 2
[Space 3]
Interactive exercises for Module 2 and beyond require an active plan.