Nested loops, column overwrites row?

Oct 22, 2018 at 8:31pm


I know the answer for this doesn't change anything, I just want to know how it works...

I'm just starting with c++ and looking at basic nested loops at the moment.
My understanding is that the outer loop writes the first # in each row, at which point the inner loop writes the 2nd, 3rd, 4th and 5th # before ending line and looping back to the outer, row loop for its second run and so on.
I wanted to know for the following script does
the inner loop overwrite the 1st column on each run which has been already been executed by the 'row' outer loop?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

//5X5 # Square 
#include <iostream>
using namespace std;
int main ()
{
for (int row =1; row <=5; ++row) {
for (int col =1; col <=5; ++col) {
cout << "#"; }
cout <<endl;}
return 0;
}
Any replies much appreciated 

Oct 22, 2018 at 8:49pm
@Goatboy, to know how it works, try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main ()
{
    cout << "When" << endl;
    for (int row =1; row <=5; ++row) {
        cout <<" row is " << row << endl;
        for (int col =1; col <=5; ++col) {
            cout << " col is " << col << endl;
   // cout << "#"; 
        }
        cout <<endl;
    }
return 0;
}

For each value of the outer loop, the inner loop is executed in its entirety.
I think that should explain it.

More explicit with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main ()
{
    cout << "When" << endl;
    for (int row =1; row <=5; ++row) {
        cout <<" row is " << row << " col is" << endl;
        for (int col =1; col <=5; ++col) {
            cout << col << " ";
   // cout << "#"; 
        }
        cout <<endl;
    }
return 0;
}
Last edited on Oct 22, 2018 at 8:53pm
Oct 22, 2018 at 9:15pm
Thanks @Blongho,
Is it fair to say from my example that the outer loop does not cout anything, only the inner loop does?
Thanks
Oct 22, 2018 at 9:56pm
Your example:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main ()
{
  for (int row =1; row <=5; ++row) {
    for (int col =1; col <=5; ++col) {
      cout << "#"; // this is what the inner loop does
    }
    cout <<endl; // this output is by the outer loop
  }
  return 0;
}

The inner loop repeats only one thing (5 times): the cout << "#";

The outer loop repeats two things:
1. A for statement (i.e. the inner loop)
2. The cout <<endl;
Oct 22, 2018 at 10:27pm
Thanks keskiverto, so the couts mirror the for statements I think... Sorry, its my non scientific brain trying to make sense of it!
Oct 23, 2018 at 8:19am
1
2
3
for (int col =1; col <=5; ++col) {
  cout << "#";
}

Produces same visible result as:
1
2
3
4
5
cout << "#";
cout << "#";
cout << "#";
cout << "#";
cout << "#";

and as:
cout << "#####";

Therefore,
1
2
3
4
5
6
for (int row =1; row <=5; ++row) {
  for (int col =1; col <=5; ++col) {
    cout << "#";
  }
  cout <<endl;
}

Produces same visible result as:
1
2
3
4
5
6
7
8
for (int row =1; row <=5; ++row) {
  cout << "#";
  cout << "#";
  cout << "#";
  cout << "#";
  cout << "#";
  cout <<endl;
}

and as:
1
2
3
for (int row =1; row <=5; ++row) {
  cout << "#####" <<endl;
}

and:
1
2
3
4
5
cout << "#####" << endl;
cout << "#####" << endl;
cout << "#####" << endl;
cout << "#####" << endl;
cout << "#####" << endl;



I'm not sure what you mean by "A mirror the B". That is the hard part of communication; parties have different vocabulary.
Oct 23, 2018 at 10:49am
so the couts mirror the for statements I think

Nothing "mirrors" anything. I have no idea what that even means.

In a for loop, you define which lines of code execute within the loop; in other words, you define which lines of code will be executed every time the loop iterates.

In your inner loop, you have defined that the following code will be executed every time the loop iterates:

cout << "#";

In your outer loop, you have defined that the following code will be executed every time the loop iterates:

1
2
3
4
for (int col =1; col <=5; ++col) {
  cout << "#"; 
}
cout <<endl;

You would help yourself enormously if you adopted a clear and sensible indentation style. This would help you to see at a glance exactly how the logic of your code works.
Last edited on Oct 23, 2018 at 10:49am
Topic archived. No new replies allowed.