# Beginner question: infinite loop

• 12-11-2010
c++urious
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:

Code:

```#include <iostream> using namespace std; int main() {     int i, n;     cout << "Input a number and press ENTER: ";     cin >> n;     i = 1;     while (i <= n) {         cout << i << " ";         i = i + 1;     }     return 0; }```
....But whoops, I made an error in the first line of the while loop:

Code:

```...     while (i <= 1) ...```
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?
• 12-11-2010
c++urious
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:

Code:

```int main() {     int i, n;     cout << "Input a number and press ENTER: ";     cin >> n;     i = 1;     while (i <= 1)         cout << i << " ";         i = i + 1;     return 0; }```
• 12-11-2010
chameleons
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.
• 12-11-2010
c++urious
Quote:

Originally Posted by chameleons
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.
• 12-11-2010
EVOEx
Quote:

Originally Posted by c++urious
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.

That's how loops work: it executes only one statement. Which in your case is:
Code:

`cout << i << " ";`
Luckily, C(++) allows you to GROUP statements, so that this is only a single statement:
Code:

```{ cout << i << " "; i++; }```
So, to execute more than one statement you'll need to group them with braces (well, there are other ways, but usually this is the best way).
• 12-12-2010
c++urious
Quote:

Originally Posted by EVOEx
That's how loops work: it executes only one statement. Which in your case is:
Code:

`cout << i << " ";`
Luckily, C(++) allows you to GROUP statements, so that this is only a single statement:
Code:

```{ cout << i << " "; i++; }```
So, to execute more than one statement you'll need to group them with braces (well, there are other ways, but usually this is the best way).

I see, thanks for the explanation. Had I went ahead a couple of pages more in the book, I would have been reminded of compound statements.