Understanding...

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <iostream>
using namespace std;

void drawrectangle(int l, int h)
{
    for(int line(0); line < h; line++)
    {
        for(int columns(0); columns < l; columns++)
        {
            cout << "*";
        }
        cout << endl;
    }
}

int main()
{
    int width, height;
    cout << "Width of rectangle : ";
    cin >> width;
    cout << "Height of rectangle : ";
    cin >>height;

   drawrectangle(width, height);
    return 0;
}


Hi, I have a little question about that code : what does the line++ exactly do ?? I know that after endl we restart the loop. But why do we need line++ ??? thank you !
I know what an increment is, it's just I don't understand its role in that code....
The link explains how the for loop works, incrementing is a part of that.

A brief explanation :

The for loop looks like this - psuedo code.

for (Initialisation Statement ; End Condition ; Increment Statement) {}

The variable in the Initialisation Statement is incremented by the Increment Statement until the End Condition becomes false.

1
2
3
4
const int LIMIT = 10;
for(int a = 0; a < LIMIT ; a++) {
     std::cout << a << std::endl;
}
Ok, but in my case, what is code incrementing exactly after the endl ? I don't understand what needs to be incremented...is it the incrementation a number given to each horizontal line ???
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:

1
2
3
4
*****
*****
*****
 
Last edited on
Ah ok... Now it's clear. One last question, I've seen that without the line++ inrementation, the loops repeats to infinty. Tell me if the way I see it is correct : Is it because of the counter of the programm which at each end of loop never increments, so the limit in line < h is never reached, so it keeps running for ever ? Is this correct ? thank you !
That is exactly right and you can increment by more than one at a time if required, line++ adds 1 to the value. If you wanted (although unneccasary in this code) you could increase by any amount per loop using line+=2 which increments by 2 instead.

Probably pointless me telling you that but replying with 'yes' as a comment would be a bit dull ;)
Topic archived. No new replies allowed.