You have 2 loops here.
The outer loop runs for each line. The inner loop runs for each column.
Let's start with the inner loop:
1 2 3 4
|
for(int columns(0); columns < l; columns++)
{
cout << "*";
}
| |
Here, 'columns' is a counter which keeps track of how many times the loop is running. It will start at 0, then increment to 1, then to 2, etc, up until columns reaches whatever value is in 'l'.
So if l==5, this means the loop will run 5 times: Once where columns=0, then when columns=1, then when columns=2, and so on. Once columns=5, the loop will stop.
Each time the loop body runs, you send "*" to cout, which will display it to the screen. This results in 5 *s being printed
*****
. Conceptually, this is one row of your rectangle.
Now let's look at the outer loop:
1 2 3 4 5 6 7 8
|
for(int line(0); line < h; line++)
{
for(int columns(0); columns < l; columns++)
{
cout << "*";
}
cout << endl;
}
| |
We've already established what that inner loop does... so let's remove it from the picture to keep this simple:
1 2 3 4 5
|
for(int line(0); line < h; line++)
{
// ... code to output one full row of *s here ...
cout << endl;
}
| |
This is the same idea. 'line' is another counter to keep track of how many times the loop will run. Only this time we loop until line reaches 'h'.
So if h==3, this will run 3 times, once where line=0, again when line=1, and again when line=2.
Each time the loop runs, it will output one full row of stars (per the above explained inner loop), then output endl to move to a new line.
The final result (assuming l=5, h=3) is that you will output 5 stars then a newline... and you'll do that 3 times. Giving you output that looks like this: