user3914798 user3914798 - 1 year ago 41
Java Question

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

What is the need for using the

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

Answer Source

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.