Computer Science I / Loops and Repeated Computation
Practice question · Put in order

Order the parts of a correct while loop so that it counts up and then stops.

Hints
  1. A while loop tests its condition before each pass, so the counter must exist first.
  2. The update must run inside the loop, or the condition never becomes false.
Show the answer
  1. Initialize the counter: i = 0
  2. Check the condition before the pass: is i < 5 ?
  3. Run the body of the loop
  4. Update the counter toward the condition becoming false: i = i + 1
  5. Return to check the condition again
Why

Initialize, then test, run the body, update, and loop back to test again. Omitting the update is the classic infinite loop: the condition stays true forever because nothing moves the counter toward making it false.

Read the lesson: Loops and Repeated Computation →

Practise Loops and Repeated Computation

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Loops and Repeated Computation