Are “while(true)” loops so bad? [closed]

I’ve been programming in Java for several years now, but I just recently returned to school to get a formal degree. I was quite surprised to learn that, on my last assignment, I lost points for using a loop like the one below.

do{
     //get some input.
     //if the input meets my conditions, break;
     //Otherwise ask again.
} while(true)

Now for my test I’m just scanning for some console input, but I was told that this kind of loop is discouraged because using break is akin to goto, we just don’t do it.

I understand fully the pitfalls of goto and its Java cousin break:label, and I have the good sense not to use them. I also realize that a more complete program would provide some other means of escape, say for instance to just end the program, but that wasn’t a reason my professor cited, so…

What’s wrong with do-while(true)?

21 Answers
21

Leave a Comment