Practice question · Put in order
Order what a loop does, from entry to the start of its second pass.
- Run the loop body
- Test the condition
- Initialise the counter
- Test the condition again
- Update the counter
Hints
- A while loop checks its condition BEFORE each pass, not after.
- The update is the last thing a pass does; without it the same test is repeated forever.
Show the answer
- Initialise the counter
- Test the condition
- Run the loop body
- Update the counter
- Test the condition again
Why
Initialisation, condition, body, update, condition, the three ingredients in their natural cycle. If the update is missing or moves the wrong way, the second test gives the same verdict as the first and the loop never ends.
Practise Loops and Iteration
The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.