Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I avoid an infinite loop with a `while` condition?

Quick answer

A while loop repeats as long as its condition is true, checking that condition before each pass. To make a counter loop stop, initialize the counter, compare it with a clear limit, and update it inside the loop so it can reach that limit. For example, while (count < 5) stops when count becomes 5, provided each pass increments count.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m writing a JavaScript while loop and it keeps running. What should I check first?

Geez

Check two things: what makes the condition false, and whether the loop body changes something that can make that happen. JavaScript evaluates the condition before each pass. If it is true, the body runs; if false, execution continues after the loop. [S1]

For a counter loop, the counter needs a starting value, a limit in the condition, and an update inside the body. Here is a bounded example:

let count = 0;

while (count < 5) {
  console.log(count);
  count += 1;
}

The stopping condition is count < 5 becoming false. This example prints 0 through 4. After the fifth pass, count is 5, so the next condition check fails and the loop ends.

Follow-up question

Can you walk through why that loop stops, rather than just saying it increments?

Geez

Sure. The loop checks count < 5 before each pass. Initially, count is 0, so the comparison is true. The body runs and adds 1. The next check uses the new value, and the same pattern continues.

The values at the start of the passes are 0, 1, 2, 3, and 4. Each pass moves the counter closer to 5. After the pass that starts with 4, the update makes count equal to 5. The next check is 5 < 5, which is false, so there is no sixth pass. A bounded counter loop like this terminates because its update lets the counter reach the point where its condition is false. [S1]

Follow-up question

What kind of mistake would make this loop keep running?

Geez

If you remove count += 1, the value stays at 0. Since 0 < 5 remains true, the loop keeps taking passes without getting nearer to its stopping point. The same risk applies if an update moves the counter the wrong way, such as subtracting 1 while the condition is count < 5.

When debugging, trace the values used by the condition at the start of a few passes. Ask whether the body changes those values and whether the changes move toward making the condition false. A condition that depends on a value the body never changes may stay true indefinitely. [S1]

Follow-up question

Is there a simple way to design and check my own counter loop before I run it?

Geez

Write the loop in this order: initialize, test, do the work, update. Then state the stopping point in a sentence. For the example, that sentence is: “Stop when count reaches 5.” Check that the initial value does not already make the condition false, unless zero passes are intended, and that the update actually moves toward the stopping point.

You can also make a tiny trace on paper: record the counter value at the start of each pass, apply the update, and check the condition again. If the values repeat or head away from the limit, revise the condition or update. This check helps with counter loops; it does not establish that every possible while loop will terminate. The key is to inspect the particular state and condition your code uses.

A practical checklist

  • Write down the value that the while condition checks.

  • Choose and initialize a starting value before the loop.

  • State the stopping condition in plain language.

  • Update the relevant value inside each pass, moving it toward the stopping point.

  • Trace a few passes and confirm the condition eventually becomes false.

A few more useful answers

Does `while` check its condition before or after the loop body?

Before each pass. If the condition is false at the first check, the body does not run. [S1]

Can a loop stop if its body does not change the counter?

Not if the condition stays true and no other relevant state changes. In a counter loop, update the counter so it can reach the stopping point. [S1]

Does this counter example prove that any `while` loop will stop?

No. It shows why this particular bounded counter loop stops. Other loops need their own condition and progress checked.

Sources

References used for this guide.

  1. [S1] while — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work