Using Braces in C++ [modified]
-
Is there a reason for using braces if they are not part of a condition or loop? For example: cameraCollection->Item(0, &camera); { //extra code... } In this example, will it make a difference if I take out the braces? if it does, why? Also, I have a sample code with no braces in the for loops? I thought braces were required, but the code compiles and does what it is supposed to do. Example: for(int y=0; y<288; y++) for(int x=0; x<353; x++) brightnessSum+=frameBuffer[x+353*y]; modified on Monday, June 2, 2008 9:08 AM
-
Is there a reason for using braces if they are not part of a condition or loop? For example: cameraCollection->Item(0, &camera); { //extra code... } In this example, will it make a difference if I take out the braces? if it does, why? Also, I have a sample code with no braces in the for loops? I thought braces were required, but the code compiles and does what it is supposed to do. Example: for(int y=0; y<288; y++) for(int x=0; x<353; x++) brightnessSum+=frameBuffer[x+353*y]; modified on Monday, June 2, 2008 9:08 AM
I use them for "local" logical section in a function/method to keep variable close to what they do. adding braces does not make a difference UNLESS you define a variable in the scope of the braces and either try to use it after or forgetting that you used it before, it can be confusing to you.
int myInt = 0;
{
int myint = 1;
}cout << myInt;
This signature was proudly tested on animals.
-
Is there a reason for using braces if they are not part of a condition or loop? For example: cameraCollection->Item(0, &camera); { //extra code... } In this example, will it make a difference if I take out the braces? if it does, why? Also, I have a sample code with no braces in the for loops? I thought braces were required, but the code compiles and does what it is supposed to do. Example: for(int y=0; y<288; y++) for(int x=0; x<353; x++) brightnessSum+=frameBuffer[x+353*y]; modified on Monday, June 2, 2008 9:08 AM
Queeny wrote:
In this example, will it make a difference if I take out the braces?
No. If you had any variables declared within the braces, they would go out of scope at the closing brace.
Queeny wrote:
Also, I have a sample code with no braces in the for loops? I thought braces were required, but the code compiles and does what it is supposed to do.
for
/while
/if
statements will use the next statement (i.e., single) if no braces are present. The following statements are equivalent:for(int x=0; x<353; x++)
{
brightnessSum+=frameBuffer[x+353*y];
}for(int x=0; x<353; x++)
brightnessSum+=frameBuffer[x+353*y];"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Is there a reason for using braces if they are not part of a condition or loop? For example: cameraCollection->Item(0, &camera); { //extra code... } In this example, will it make a difference if I take out the braces? if it does, why? Also, I have a sample code with no braces in the for loops? I thought braces were required, but the code compiles and does what it is supposed to do. Example: for(int y=0; y<288; y++) for(int x=0; x<353; x++) brightnessSum+=frameBuffer[x+353*y]; modified on Monday, June 2, 2008 9:08 AM
Queeny wrote:
Is there a reason for using braces if they are not part of a condition or loop? For example:
Yes - for finer control of the construction and destruction of objects. For example:
//
// Some Code Getting Ready To Process Something...
//
{
CMyHeavyweight myobjObject;
myobjObject.DoYerThang( true );
}
//
// Some Code After Processing Something...
//Even if the object is not too heavyweight, you may have a reason to want/need to limit the time it exists because it manages some shared resource(s). Yes, this is usually a sign of bad design, but you may be using a pre-existing library that you have no control over. Peace!
-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
Is there a reason for using braces if they are not part of a condition or loop? For example: cameraCollection->Item(0, &camera); { //extra code... } In this example, will it make a difference if I take out the braces? if it does, why? Also, I have a sample code with no braces in the for loops? I thought braces were required, but the code compiles and does what it is supposed to do. Example: for(int y=0; y<288; y++) for(int x=0; x<353; x++) brightnessSum+=frameBuffer[x+353*y]; modified on Monday, June 2, 2008 9:08 AM
I just want to add one more point. Its always recommenced, in "Maintenance" point of view. I'm taking "if" as an e.g.. If the "if" block is a single line, braces are not necessary. But in future if someone(especially beginners) needs to add another line to the "if" body, chances are lot to forget to put the braces which will end up in trouble. For instance,
if( bFailed )
statement1;
Bugfix Statement1; // Will get executed always.It won't occur often. But still a good practice. :rolleyes: Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
I just want to add one more point. Its always recommenced, in "Maintenance" point of view. I'm taking "if" as an e.g.. If the "if" block is a single line, braces are not necessary. But in future if someone(especially beginners) needs to add another line to the "if" body, chances are lot to forget to put the braces which will end up in trouble. For instance,
if( bFailed )
statement1;
Bugfix Statement1; // Will get executed always.It won't occur often. But still a good practice. :rolleyes: Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
Is there a reason for using braces if they are not part of a condition or loop? For example: cameraCollection->Item(0, &camera); { //extra code... } In this example, will it make a difference if I take out the braces? if it does, why? Also, I have a sample code with no braces in the for loops? I thought braces were required, but the code compiles and does what it is supposed to do. Example: for(int y=0; y<288; y++) for(int x=0; x<353; x++) brightnessSum+=frameBuffer[x+353*y]; modified on Monday, June 2, 2008 9:08 AM
No if your loop has statement you dont need to use of them however its better you use of them and for declare local variables if you have same name for global variables you must be use of them and ....