Unable to open this exercise
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.
Independent Practice: Bounded powers
Read a non-negative integer limit. Output powers of two starting at 1, each on a separate line, while the value is no greater than the limit. Output nothing when the limit is zero.
Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.
Interactive exercises for Module 2 and beyond require an active plan.
Run code to inspect step-by-step variable trace states.