Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-29-2005, 11:57 AM   #1 (permalink)
357mag
Registered User
 
Join Date: Mar 2005
Posts: 15
357mag is on a distinguished road
What's goin on with these braces?

I've written a program that inputs the number of rows and columns, and which character to print and then prints a matrix. The code works fine, but I did some experimenting with removing braces like this:

for(int i = 0; i < rows; i++)
// opening brace would go here
for(int j = 0; j < columns; j++)
cout << theChar;
cout << "\n";
// closing brace would go here
return 0;

Like I said if I don't include the braces it will print like this:

**************** and so on.

I think that's because the body of the for loop expects only one statement. And the body of the inner loop actually has two statements in it. So when the inner loop is done printing 5 * characters instead of going to the "\n statement instead it goes to the outer loop again increments 0 to 1 and then it goes to the inner loop again and starts over. Correct me if I'm wrong.

Now if I do the opposite and add braces to the inner loop, so both the outer and inner loop have braces so the code looks like this:

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

return 0;

Now it will print like this:

*
*
*
*
*
*
and so on.

Is that because with the addition of the inner braces both statements are executed in succession? Like it will output a * character and then go to a newline. Then output a * character and then go to a new line. Then output a * character... and so on?

I think I may be on the right track here but want to know for sure.
357mag is offline   Reply With Quote
Old 04-29-2005, 12:12 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Hey mag. Now edit your code and add the "code" tags. Learn it now .
And after that I will see what I can do for you .
__________________
Valmont is offline   Reply With Quote
Old 04-29-2005, 04:08 PM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
to put it like you do:
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cout << theChar;
}
cout << "\n";
}

return 0;
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-29-2005, 10:46 PM   #4 (permalink)
357mag
Registered User
 
Join Date: Mar 2005
Posts: 15
357mag is on a distinguished road
What is a code tag?
357mag is offline   Reply With Quote
Old 04-30-2005, 12:26 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
Originally Posted by 357mag
What is a code tag?
it's using [code ]
int main(){
// your code here
}
[/code ]
Which will show it like:
Code:
int main(){
// your code here
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-30-2005, 05:19 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
[READ THIS FIRST] About the standard C++ forum.
__________________
Valmont is offline   Reply With Quote
Old 04-30-2005, 12:12 PM   #7 (permalink)
357mag
Registered User
 
Join Date: Mar 2005
Posts: 15
357mag is on a distinguished road
Why would you type your code between two brackets? The only thing brackets are used for is arrays. You would not type:

int main()
[cout << "Hello World!"]

I'm not following.
357mag is offline   Reply With Quote
Old 04-30-2005, 12:19 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Read redhead's statement again. And read the link in my reply.

Place your actual code that you post here between the tags so it will be formatted neatly by this forum.
And read this:
[READ THIS FIRST] About the standard C++ forum.
__________________
Valmont is offline   Reply With Quote
Old 04-30-2005, 12:26 PM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
the "brackets" are the [code ] and [/code ] tags but without the spaces befor the closing brackets.
the keyname here is code, when the vBulletin viewer reads the text to show for you, it will look for a start bracket '[' then look for a keyword, in this case code then look for a end bracket ']', once this matches it will show everything written after that in a special way, once it encounters an ending tag beginning with a start bracket '[', a slash '/' and the keyword it previus found, here code is looked for again, ending with a closing bracket ']' it will stop showing the text in a specific way..
Thus you will write [code ] then your intended code and end your code with [/code ] (without the spaces betwenn the keyword and the brackets)
When doing so, the code will appear more readable since indentation is kept as written, which in terms makes it easier to read and trouble shoot.
In order to see this in action, you can click the add code button in the advanced posting section, then you will see how it automaticaly adds the [code ] and [/code ] around the text which is written as intended code.

I hope this clarifies it a bit more.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-01-2005, 12:50 AM   #10 (permalink)
357mag
Registered User
 
Join Date: Mar 2005
Posts: 15
357mag is on a distinguished road
Where is the advanced posting section? I don't see it. And this is the first forum I've come across that asks you to use those code tags.
357mag is offline   Reply With Quote
Old 05-01-2005, 06:59 AM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
You can type it as well. It's just like programming.
Do not put spaces between the code tags and the brackets by the way.
Edit your post (press the edit buttong) and place your code snippets between the code tags.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
formatting sde Program Design and Methods 35 04-03-2003 01:12 PM


All times are GMT -8. The time now is 06:43 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting