user3914798 user3914798 - 6 months ago 10
Java Question

Difference between for and while loop(not in terms of syntax and efficiency)

What is the need for using the

while
loop when we can iterate without using
while
? Is there something that cannot be done without
while
?

Answer

There is nothing that while can do that you wouldn't be able to do with for. In fact,

while (xxxxxxx) {
}

can be replaced mechanically with

for (; xxxxxxx ;) {
}

and the code would work exactly the same.

The only reason for having the while loop as well is better readability of your code.