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

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

Validate the type of input in a do-while loop C

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

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