Creating a perpetual loop in Java

TUTOR HOME
PREVIOUS
TIPS HOME
NEXT
HELP BUY CONTACT ABOUT

Java won't allow a simple loop such as

while (true) {}

since any code following the loop will be unreachable (doh!). Java considers this an error, not a warning. (I don't agree, but they didn't ask me.) You can fool Java into doing this:

boolean flag=true;
while (flag) {}

if there is a way to break the loop, Java won't complain:

while (true) {

if (...) break;

}

- thanks to Ralph Iden -