While Loops

The Three Steps of a Loop

A while loop repeats its body as long as a condition holds, checked before every pass, including the first:

int attempts = 0;             // 1. initialize
while (attempts < 3 && !connected) // 2. test
{
    connected = tryConnect();
    attempts++;                // 3. update
}

Every loop needs these same three steps — initialize the loop variable, test it, update it inside the loop. A for loop (Lesson 5.1-5.2) just packs all three into one header line; a while loop spreads them out, which is more natural when the "update" isn't a simple counter.

Infinite Loops

Forget step 3 and the condition never changes — the loop runs forever:

int i = 0;
while (i < 10)
{
    System.out.println(i); // i never changes — this never stops
}

An infinite loop on a robot doesn't just print forever — it can freeze the whole robot's control loop, since nothing after it ever gets a chance to run.

When to Reach for while Instead of for

for fits naturally when you already know the number of iterations. while fits better when you're repeating until a condition changes, not counting to a number — like retrying a connection a limited number of times:

int attempts = 0;
while (!gyro.isConnected() && attempts < 5)
{
    gyro.reconnect(); // assume our gyro class has this retry method
    attempts++;
}

This tries at most 5 times, then moves on whether or not the gyro came back. What it must never do is wait: robot code runs in short, repeating cycles many times per second, and a loop that sits spinning until a sensor finally responds holds up everything else — the freeze described above. In robot code, check a condition once per cycle instead of looping until it changes.

do-while: Run the Body at Least Once

A do-while loop checks its condition after each pass instead of before, so the body always runs at least once:

int attempts = 0;
do
{
    connected = tryConnect();
    attempts++;
} while (!connected && attempts < 3); // this semicolon is required

This is the first loop in this lesson, reshaped: the first connection attempt happens unconditionally, and the test only decides whether to try again. Use do-while when there's nothing to test until the body has run once; otherwise, a plain while is the usual choice.

Leaving a Loop Early: break and continue

Two statements change a loop's flow from inside its body. break ends the loop immediately; continue skips the rest of the current pass and moves on to the next one:

for (int port = 0; port < 10; port++)
{
    if (port == 3)
    {
        continue; // port 3 is reserved: skip to the next pass
    }
    if (isFree(port))
    {
        System.out.println("First free port: " + port);
        break;    // found one: stop the whole loop now
    }
}

In a for loop, continue still runs the update (port++) before the next test; in a while loop, it jumps straight to the test. Inside nested loops, both affect only the innermost loop they're in. (break also ends a case in a switch statement — Lesson 5.12.)

Common Pitfalls

  • Forgetting to update the loop variable. The single most common cause of an infinite loop — every while loop needs something inside it that eventually makes the condition false.
  • A condition that's false from the very first check. If the condition never holds, the loop body never runs at all — not even once. Double-check the loop variable's starting value against the condition.
  • Leaving off the semicolon after a do-while's condition. } while (attempts < 3) with no ; won't compile. The opposite mistake, a stray ; right after a plain while (...) header, gives the loop an empty body — and an infinite loop if the condition starts true.
  • A continue in a while loop that skips the update. If the update (i++) comes after the continue, the skipped pass never reaches it, and the loop repeats the same value forever.

Key Takeaways

  • Every loop needs three steps: initialize, test, update — while just doesn't bundle them into one line like for does.
  • A while loop's condition is checked before every pass, including the first — if it starts false, the body never runs.
  • Forgetting to update the loop variable is the classic cause of an infinite loop, which can freeze a robot's control code.
  • while fits naturally when you're repeating until a condition changes rather than counting a known number of iterations.
  • A do-while checks its condition after each pass, so its body always runs at least once — and its closing while (...) needs a semicolon.
  • break ends the innermost loop immediately; continue skips to that loop's next pass.

Derived from csawesome-2026: unit-2-selection-and-iteration/2.07-while-loops.md (CSA 2.7)