javaloopswhile-loop

What condition does while(true) test? When is it true and false?


I do no understand why anybody would use

while(true) {
   //do something
}

instead of

boolean condition = true;
while(condition == true) {
   //do something
}

The latter is super easy to understand, whilst the former is not.

So, what condition does while(true) check? When is while(true) true, and when is it false?


Solution

  • When is while(true) true, and when is it false?

    It's always true, it's never false.

    Some people use while(true) loops and then use break to exit them when a certain condition is true, but it's generally quite sloppy practice and not recommended. Without the use of break, return, System.exit(), or some other such mechanism, it will keep looping forever.