I have this program which is supposed to be stuck in an infinite loop at line 12. But it doesn't, instead it prints 5. It only behaves correctly (i.e runs infinitely) when I run it in debug mode.
It never gets in the first loop starting at line 6, which is the intention. Am I missing something?
Joseph Mokenela wrote:It only behaves correctly (i.e runs infinitely) when I run it in debug mode.
That's very unlikely - it's not infinite. In this case, it just loops very many times, which seems infinite to you when you go through the debugger. But the computer can go through all those loops much more quickly, when you're not slowing it down to debug. The number is actually changing a little bit each time... you have to think about, when might it eventually leave each loop, and why?
I suggest analyzing smaller parts of the problem first, to work your way up in understanding. Can you figure out what this code will do, and why?
Then, can you figure out what this will do, and why?
After you understand those, try looking again at the original code.