Sunday, May 25, 2025

The While-Loop

It’s very natural in computer science to want to do something over and over again. It’s for this reason that loops exist. Java has 4 loops, and we’ll cover one each in the next 4 articles: the while, the do-while, the indexed-for, and the for-each.


Syntactically, the while is the simplest. Logically, it’s the easiest to understand. We’ll start with it.

In plain English, “check if something is true, then do X; check if something is true, then do X; check if something is true, then do X.”

While loops look like this:

while(condition){
statements;
}

This should look very familiar, since if-statements are nearly identical. In fact, one of the ways you might hear about a while loop in a formal computer science course  is as  a repeated if-statement. You check the condition, then you do the thing, then you check the condition again, and you do the thing…

The same rules about braces apply to while loops. But, again, my recommendation is to always use them.

Imagine we want to sum all the numbers from 0 to (and including) 10.

We could set up a WhileLoopPractice class, and inside main say something like

int res = 0;
res+=1;
res+=2;
res+=3;

And so on…
and eventually come up with the (correct) answer 55.

But one of the benefits of a while loop is that we don’t need to be that verbose.

in that example, we could say:

int i = 0;
int res = 0;
while (i <= 10){
     res+=i;
}

If we then print out res after the loop finishes, it will have the right result.

Let’s look more closely at the bare-bones structure of a while-loop.

There is an iterator, a counter of some sort—in the sum example, we called it i. There is a condition being checked—in this case, whether i is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, or 10. And there is at least one statement to be executed as long as the condition evaluates to true. Any valid Boolean expression can be that condition, just as was the case for if-statements.

Right out of the gate, there are two common bugs you might face:

  • What if a loop you intended to run never executes?
  • What if a loop you intended to run for a while and then have stop never stops?

It is easy to produce examples of both.

Scenario 1 looks like this:

int i = 0;

while(i < 0 && i > -10){
      System.out.println(“I’m negative!”);
     i--;
}

Notice here that i is set to 0. The condition specifies that i must be strictly less than 0, and strictly greater than -10.

But i is 0, which is not strictly less than 0. And given how we know AND works—both things fed to && must be true for && to return true—the very first check will fail and we’ll just skip over the loop. That loop, which we intended to print “I’m negative!” 10 times never actually executes.

Scenario 2 looks like this:

int i = 0;

while(i < 100){
     System.out.println(“I’m happy!”);
}

Look at what’s happening here: i is set to 0.
Is i’s value less than 100? “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”
Is i’s value less than 100? Yes “I’m happy!”

on and on forever

We’re never actually changing the value of i, so since it was initially less than 100, it will always be less than 100. This loop, in contrast to the other one we just saw that never started, never ends.

Writing this kind of loop by mistake is totally normal—and, for every programmer, doing this for the first time is a rite of passage. Thankfully, it’s a really easy mistake to fix; don’t sweat it!

All that needs to be done is, inside the loop, update i’s value.

Now, the fixed loop looks like:

int i = 0;

while(i < 100){
     System.out.println(“I’m happy!”);
     i++;
}

now, there’ll be an iteration where we print “I’m happy!” when i is 0, 1, 2, 3, 4, 5, …, 97, 98, and 99, and then, crucially, when I gets incremented to 100, it will no longer be true that 100 < 100, so the loop will stop. We’ll print “I’m happy!” exactly 100 times and then stop.


No comments:

Post a Comment

Switch

 Other than if/if-else/if-else if-else and the ternary operator, there is yet another common and important conditional expression in Java th...