Java: Infinite Loop Convention [closed]

There is no difference in bytecode between while(true) and for(;;) but I prefer while(true) since it is less confusing (especially for someone new to Java). You can check it with this code example void test1(){ for (;;){ System.out.println(“hello”); } } void test2(){ while(true){ System.out.println(“world”); } } When you use command javap -c ClassWithThoseMethods you will … Read more

Validate the type of input in a do-while loop

The problem is that “scanf()” can leave unread data in your input buffer. Hence the “infinite loop”. Another issue is that you should validate the return value from scanf(). If you expect one integer value … and scanf returns “0” items read … then you know something went wrong. Here is an example: #include <stdio.h> … Read more

Endless loop in C/C++ [closed]

The problem with asking this question is that you’ll get so many subjective answers that simply state “I prefer this…”. Instead of making such pointless statements, I’ll try to answer this question with facts and references, rather than personal opinions. Through experience, we can probably start by excluding the do-while alternatives (and the goto), as … Read more