Are compilers allowed to eliminate infinite loops?

C11 clarifies the answer to this question, in the draft C11 standard section 6.8.5 Iteration statements added the following paragraph: An iteration statement whose controlling expression is not a constant expression,156) that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in … Read more

Prolog successor notation yields incomplete result and infinite loop

If you want to study termination properties in depth, programs using successor-arithmetics are an ideal study object: You know a priori what they should describe, so you can concentrate on the more technical details. You will need to understand several notions. Universal termination The easiest way to explain it, is to consider Goal, false. This … Read more

How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner

As per the javadoc for Scanner: When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method. That means that if the next token is not an int, it throws the InputMismatchException, but the token stays there. … Read more

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