View Single Post
Old Apr 29th, 2005, 12:41 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Just like to know why

This code asks the user how many rows he would like and how many columns he would like plus what character he wants to print. Then it uses a nested for loop to print the output:

int main()
{
int rows, columns;
char theChar;

cout << "How many rows? ";
cin >> rows;
cout << "How many columns? ";
cin >> columns;
cout << "What character? ";
cin >> theChar;

for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
cout << theChar;

cout << "\n";
}

return 0;
}

Now the code works fine. But if I remove the opening brace and closing brace for the for loop, and say input 5 for rows and 5 for columns it will print like this:

*************************

I'd like to understand why it does that. Is it because there are actually 2 statements in the body of the inner loop? And without the braces only the
cout << theChar statement will run? Just like to be clear on this.
357mag is offline   Reply With Quote