Beginner question: infinite loop

This is a discussion on Beginner question: infinite loop within the C++ Programming forums, part of the General Programming Boards category; My interest in learning programming was recently rekindled, and so I picked up a copy of C++ Without Fear, recommended ...

Beginner question: infinite loop

My interest in learning programming was recently rekindled, and so I picked up a copy of C++ Without Fear, recommended on the beginner list at this site. I've ran into a question for which I can't figure out the answer to on my own. The example code is:

When running this flawed code, it seems to me that the while loop is stuck on the line

Code:

cout << i << " ";

, as a continuous stream of 1's is printed. If any of you can explain to me exactly what is happening, that would be great. In my beginner mind, this flawed while loop should run only once. What am I missing?

I stepped away for a while, came back, and partially figured out what happened. I had written the example without the necessary brackets for the while loop, originally. I noticed my mistake and made the changes, but I forgot to recompile after the changes. I'm still not sure why this is looping and printing 1's:

well, you forgot to enclose the two statements in the while loop with curly brackets. As such, only the first statement is considered to be in the while loop, whereas the statement where i increases is not. Therefore the infinite loop.

well, you forgot to enclose the two statements in the while loop with curly brackets. As such, only the first statement is considered to be in the while loop, whereas the statement where i increases is not. Therefore the infinite loop.

I'm not sure why the second line of the loop doesn't run when the braces are missing, but thanks. At least I now know what is happening.