Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Apr 29th, 2005, 1:18 AM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
it's just executing the next one line of code which is the second nested loop statement. the /n character is ignored until the very end of the program.

that's why we have those pretty braces!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 29th, 2005, 1:36 PM   #3
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I noticed that if I add two more braces like this:

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

It will print just the opposite like this:
*
*
*
*
*
*
and all the way down to 25.

Why?
357mag is offline   Reply With Quote
Old Apr 29th, 2005, 5:06 PM   #4
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
cuz you print the "*" and then new-line it 25 times. you need to print your whole inner loop before executing the \n and then return to the next iteration of the outer loop.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC