...

Exiting a Loop

There are two very important parts in a while loop:

  • The condition itself.
  • Something in the loop body that eventually makes the condition false.

If we forget to change the variable used in the condition, the loop will run forever! This is called an infinite loop.

Your Task

The code sample has an infinite loop. Can you fix it?

  1. First, run the code sample step-by-step to see how the loop never ends.
  2. Then, add a line of code inside the loop body to decrease the count variable, so the loop eventually ends.

Variables

No variables

Loading...

Output (Console)