Thread: Beginner question: infinite loop

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    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?
    Last edited by c++urious; 12-11-2010 at 03:48 AM.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    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;
    }
    Last edited by c++urious; 12-11-2010 at 05:16 AM.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    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.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by chameleons View Post
    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.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by c++urious View Post
    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).

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by EVOEx View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  2. question about for loop
    By panfilero in forum C Programming
    Replies: 3
    Last Post: 09-27-2005, 05:59 AM
  3. Newbie Loop Question.
    By SlyMaelstrom in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2004, 03:47 PM
  4. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 03:24 PM
  5. infinite loop kicks in
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-15-2002, 04:06 PM