Nested For statement which prints astrix

Can someone explain how this code works please? The output is 15 rows and 30 columns of * but I don't understand how does it print 15 columns and 30 rows? I just don't get it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream> 
 
using namespace std; 
 
int main() {  int a, b; 
 
 
 for (a=0; a < 15; a++)  
{
  for (b=0; b < 30; b++)   
{
   cout << "*";     // Print * (asterisk)   
} 
 
  cout << endl;  
} 
 
 cout << endl; 
 
 system("PAUSE"); 
  return 0; } 
 
1
2
3
4
5
6
7
8
9
10
11
12
// print 15 rows
for (a=0; a < 15; a++)  
{
	// print 30 stars in a row
	for (b=0; b < 30; b++)
	{
		cout << "*";
	} 
	
	// end of row
	cout << endl;  
}
Renaming variables could help:
1
2
3
4
5
6
7
8
9
10
11
// print 15 rows
for ( int row=0; row < 15; ++row )  
{
  // on 'row', do:
  for ( int col=0; col < 30; ++col )
  {
    // on 'col' of 'row', print one star
    cout << "*";
  }
  cout << endl;  
}
Last edited on
But why does it print 30 stars and 15 rows if the row is less than 15, not equal to 15? and how does it know to do 'on row', is this is because it's inside it?
Last edited on
I would suggest you to do a dry run on your code if you possibly can..

Remember, in these shape structures, the outer loop is mostly always for the total number of rows.. Whereas the inner loops are for columns or to print asterisks.

From your code, notice the first outer loop,
1
2
3
4
5
for (a=0; a < 15; a++)
{
......
cout << endl // this cout << endl gives you the answer to your question that how does it know when to do row
}


You can see that at the end of the loop, there is an endl statement which tells the program to go the next line... As the loop will run for a total of 15 times, the endl statement will also execute 15 times so basically it will give you 15 rows...


It's similar for the columns.. Notice the inner loop,
1
2
3
4
5

  for (b=0; b < 30; b++)   
{
   cout << "*";     // Print * (asterisk)   
} 


As you can see, that this loop will run for a total of 30 times every time... So, it will print 30 asterisks or in other words, will print a total of 30 columns.



EDIT: To your last question that why does it do 15 rows when the value is less than 15... That's because that the loop controlling variable (i.e. a in your program) is starting from 0... So loop runs from 0-14 which is equal to 15 times in total... If you simply change a value to a = 1 and make it a <= 15 then the program will perform the same function as it is performing right now
Last edited on
Right, I finally understand the for (a=0; a < 15; a++) but I'm still confused by the 30 bit. It's difficult trying to explain what I actually don't understand.. I think it's just trying to understand how/why it prints the 30 columns. I know about cout and everything it's just the 30 columns.

I also understand why it prints 15 (I think I got so stressed trying to understand it, I wasn't thinking straight).. Thanks for that.
Last edited on
Lets try with smaller amount:
1
2
3
4
for (b=0; b < 4; b++)   
{
   cout << "*";
}

Is it clear that the loop repeats exactly 4 times?
1
2
3
4
5
6
7
8
// on first iteration b==0 and you do:
cout << "*";
// on second iteration b==1 and you do:
cout << "*";
// on third iteration b==2 and you do:
cout << "*";
// on fourth iteration b==3 and you do:
cout << "*";

In other words:
1
2
3
4
5
6
7
8
9
10
for (b=0; b < 4; b++)   
{
   cout << "*";
}

// does the same as:
cout << "*";
cout << "*";
cout << "*";
cout << "*";

What do you expect to get from:
1
2
3
4
cout << "*";
cout << "*";
cout << "*";
cout << "*";

Is it
*
*
*
*

or
****

Please try to explain why you expect one rather than the other.
Topic archived. No new replies allowed.