x starts 5. Then, when you go through it, it goes to 6, 7, 8, etc. In the end, it falls into the highest possible int value. The next x = x + 1 sets it to the most negative int, the negative 2 billion - independently. This is less than 3, so a message is displayed. Then you fulfill the while condition again, which now fails, exiting the loop.
So, although it is an infinite loop, in fact it is not.
Is this a home problem? Why did you write such odd code?
source
share