DEV Community

Mackenly Jones
Mackenly Jones

Posted on • Originally published at crtv.dev on

Why are there other loops if all loops can be implemented using a while loop in Java?

Why are there other loops if all loops can be implemented using a while loop in Java?

Java, like most programming languages, provides different types of loops. The most common are while, for, and do-while, but you can also use a for-each loop, which makes looping over data easier. Even though all of these can be implemented using a while loop, it would mean programmers would have to employ a more complicated syntax to write the same functionality. The other loop types make code more readable and easier to write.

If you ever study boolean algebra or are interested in electronics, you'll likely learn that all logic can be represented using NAND gates. However, when creating logical expressions, we as humans rarely only use NAND gates. Instead, we write our logic in easier-to-think-of gates/functions such as AND, OR, and NOT and let the computer abstract away our expressions into NAND gates for the hardware. Much in the same way, Java provides programmers with extra loops that abstract the functionality giving us keywords that are easier to write and understand.

Why are there other loops if all loops can be implemented using a while loop in Java?
NAND Truth Table via Wikipedia

The three common loop types are:

While Loop:

As previously mentioned, this loop can be used for any use case where you'd want to use a loop. While loops are often paired with a boolean variable that acts as a flag, but can directly evaluate boolean expressions in their condition.

// do while example
boolean condition = false;
do {
  System.out.println("This is a do while loop");
} while (condition);

// Outputs:
This is a do while loop
Enter fullscreen mode Exit fullscreen mode

For Loop:

Often, the first loop programmers learn, this loop is great for doing something a certain number of times while giving the programmer access to the current iteration/index number. Notice the use of the index value:

// for loop example
for (int i
  System. out. print "1 IS
} // end for loop

// Output:
i is: 0
i is: 1
i is: 2
i is: 3
i is: 4
i is: 5
i is: 6
i is: 7
i is: 8
i is: 9
Enter fullscreen mode Exit fullscreen mode

Do-while Loop:

Pretty much the same as a while loop, but the code will execute once before evaluating the condition. Only subsequent iterations are based on the condition's result. Notice that the condition is false, but it still printed once:

// while loop flag example
boolean flag = true;
int count = 0;
while (flag) {
  System.out.println("Hello World");
  count++;
  if (count == 10) {
    flag = false;
  }
}

// Output:
Hello World
Enter fullscreen mode Exit fullscreen mode

If someone were to argue that it's pointless to have multiple types of loops when you can do it all with a while loop, I would ask why are they using Java when they could be writing in assembly or even full-on binary if they're a true believer. Programming is all about abstraction. Programming languages are there to make writing code easier and more human-friendly, so adding syntax features that lower the programmer's cognitive load should be the goal of any language.

Furthermore, selecting the correct loop construct goes beyond syntax and code brevity considerations. It is also a way of expressing the developer's intent more meaningfully. A "for" loop typically indicates a known number of iterations, whereas "while" and "do-while" loops suggest a loop duration that is contingent upon a certain condition being met. This clarity of intent is crucial for code maintainability and interpretability. You could convert all loops to "while" loops, but the loss of semantic clarity and simplicity might outweigh the minimal space saved.

Top comments (0)