Thread: Different results in for loops.

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    Different results in for loops.

    I was wondering why the following two pieces of code give different results. The first one prints "theChar" a certain amount of times in a vertical line while the second creates a matrix.

    The difference is the brackets. But why does it execute the way it does?
    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        int rows, columns;
        char theChar;
    
        cout << "How many rows? ";
        cin >> rows;
        cout << "How many columns? ";
        cin >> columns;
        cout << "What character do you prefer? ";
        cin >> theChar;
    
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
                {
                cout << theChar;
                cout << endl;
                }
        }
    
        return 0;
    }
    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        int rows, columns;
        char theChar;
    
        cout << "How many rows? ";
        cin >> rows;
        cout << "How many columns? ";
        cin >> columns;
        cout << "What character do you prefer? ";
        cin >> theChar;
    
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
    
                cout << theChar;
                cout << endl;
    
        }
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If we add in the braces for the second snippet, we might write:
    Code:
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cout << theChar;
        }
    
        cout << endl;
    }
    Can you understand the difference now?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    62
    Ow wait I think I got it.

    cout << endl; is part of the first for loop. I kept looking at it as part of the second for loop...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kitt3n
    The way I see it at the moment is that in both cases the statements are inside the second for loop.
    When braces are not used to denote a block that is the body of the loop, the loop body is only the single statement following the start of the loop. Therefore, for the second case, it is simply not true that the cout << endl; is in the body of the inner for loop. My use of braces illustrates that.

    EDIT:
    Quote Originally Posted by Kitt3n
    In both cases I see it as that when the amount of columns is reached it starts the new row. But with the brackets it seems to execute the std::endl every time the char is printed.
    Consider:
    Code:
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cout << theChar;
        }
    
        cout << endl;
    }
    Observe that the inner for loop basically prints theChar columns number of times. Once that loop is done, cout << endl; is executed, and then the outer for loop continues. Therefore, this does the "when the amount of columns is reached it starts the new row" thing that you have in mind. Now, consider:
    Code:
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cout << theChar;
            cout << endl;
        }
    }
    Observe that the inner for loop still prints theChar columns number of times. But now, after each printing of theChar, cout << endl; is executed. If you just do a little tracing of the code, it should be clear to you why this prints a series of theChar in a single column.
    Last edited by laserlight; 05-25-2010 at 01:54 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    62
    Thanks for the help. I was just not fast enough editing my post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf is changing my results!
    By rogster001 in forum C Programming
    Replies: 8
    Last Post: 08-18-2009, 12:26 PM
  2. Incorrect results from fmod()?
    By karlthetruth in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 09:12 AM
  3. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 09:46 AM
  4. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  5. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM