Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Apr 29th, 2005, 1: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
 

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 11:14 PM.

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