Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using Braces in C++ [modified]

Using Braces in C++ [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
7 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Queeny
    wrote on last edited by
    #1

    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

    M D J J H 5 Replies Last reply
    0
    • Q Queeny

      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

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • Q Queeny

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • Q Queeny

          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

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • Q Queeny

            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

            J Offline
            J Offline
            Jijo Raj
            wrote on last edited by
            #5

            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.

            B 1 Reply Last reply
            0
            • J Jijo Raj

              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.

              B Offline
              B Offline
              bob16972
              wrote on last edited by
              #6

              My thoughts exactly. I've fell victim to that enough times that it's become common practice for me to always include them, in case I come back later and add to it. Good advice.

              1 Reply Last reply
              0
              • Q Queeny

                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

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #7

                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 ....

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups