Different results in for loops.

This is a discussion on Different results in for loops. within the C++ Programming forums, part of the General Programming Boards category; I was wondering why the following two pieces of code give different results. The first one prints "theChar" a certain ...

  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
    21,415
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,415
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21