CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    [RESOLVED] Is there any difference between using {} and not using them?

    I am curios of this:
    Does the computer perform more operations if it happens to execute the following code:
    Code:
    if (condition) {return true;}
    than it would execute the following code:
    Code:
    if (condition) return true;
    Or it is the same thing for C++?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Is there any difference between using {} and not using them?

    The {} is only necessary if you wish to put more than one statement inside the if.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Is there any difference between using {} and not using them?

    If there's only one statement following the if, they're the same. The braces create a code block which can include multiple statements.

    These are the same
    Code:
    if(condition)
        DoSomething();
    
    if(condition)
    {
        DoSomething();
    }
    These are not
    Code:
    if(condition)
        DoSomething();
        DoSomethingElse();
    
    if(condition)
    {
        DoSomething();
        DoSomethingElse();
    }

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Is there any difference between using {} and not using them?

    Note that even if they are necessary, it is usually a coding standard (and recommended) to always put them in anyways. The reason for this is that whenever later on you want your if to do 2 statements rather than one, there are good chances you'll forget to put them in.

    My single statements if always look like this:

    Code:
    if ( condition )
        {statement;}
    As you can see, the {} are not overly intrusive, and the indentation allows a clear flow of control. Placing the statement on a different line is important because allows a clear view of the debugger path. If I (or somebody else) wanted to add a second statement, all they'd to do is insert their statement under the first, and not worry about scope.

    Don't fool yourself into thinking "I'm so good I don't need them". Even if it is true, it might not be true for everyone on your team, and there is no reason to turn down the help.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Is there any difference between using {} and not using them?

    They are also great because IDE match them and let you fold things. Lots of times people put braces simply for that purpose.

    Good example:

    Code:
    glPushMatrix(); {
         glBegin(GL_POLY); {
              //Graphics code
         } glEnd();
    } glPopMatrix();
    If the above code, the brace do nothing. It's not a block command like if, there are no variables for there to be a scope reason. The compiled code would be exactly the same as if they weren't there. They are purely there to know what glEnd goes to which glBegin in the IDE.
    Last edited by ninja9578; September 23rd, 2010 at 02:01 PM.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by monarch_dodra View Post
    My single statements if always look like this:

    Code:
    if ( condition )
        {statement;}
    But note that many (maybe most) official coding standards require this,

    Code:
    if (condition)
    {
       statement;
    }

  7. #7
    Join Date
    Sep 2010
    Posts
    31

    Re: Is there any difference between using {} and not using them?

    And yet there are people that prefer:
    Code:
    if (condition) {
        //codey codey
    }
    or
    Code:
    if (condition)
        {
        //codey codey
        }
    It really depends on the environment you are working in.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Is there any difference between using {} and not using them?

    I don't like either of those. I prefer
    Code:
    if (condition)
    {
        // code
    }
    Much easier to visually pick out the blocks that way.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by Lindley View Post
    I don't like either of those. I prefer
    Code:
    if (condition)
    {
        // code
    }
    Much easier to visually pick out the blocks that way.
    I agree. I find the example ninja9578 posted particularly hard to read.

    Code:
    glPushMatrix(); {
         glBegin(GL_POLY); {
              //Graphics code
         } glEnd();
    } glPopMatrix();
    vs
    Code:
    glPushMatrix(); 
    {
        glBegin(GL_POLY); 
        {
              //Graphics code
        }
        glEnd();
    }
    glPopMatrix();
    Even then, I think they confuse more than they help.

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Is there any difference between using {} and not using them?

    I also prefer

    Code:
    if ( condition )
    {
        //code
        //code
        //code
    }
    However, whenever you have several ifs with a single statement, the code starts to get too voluminous, and I find it becomes clearer with a single line block+instruction.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  11. #11
    Join Date
    Sep 2010
    Posts
    39

    Re: Is there any difference between using {} and not using them?

    I recently found myself using too many {} blocks to reclaim memory of big local variables with destructors.

    Code:
    {
          ScalarField   A ;
          do something with A;
          //destructor for A
    }  
    {
          ScalarField B;
          //destructor for B
    }
    is different from
    Code:
    ScalarField A;
    ScalarField B;
    dosomethig with A;
    dosomething with B;

  12. #12
    Join Date
    Sep 2010
    Posts
    31

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by Lindley View Post
    I don't like either of those. I prefer
    Code:
    if (condition)
    { 
        // code
    }
    Much easier to visually pick out the blocks that way.
    Well, I like and prefer use the of first one, ie:
    Code:
    if (condition) {
        // code
    }
    
    //and 
    
    if (condition) {
        // code
    } else if (condition) {
        // code
    } else if (condition) {
        // code
    } else {
        // code
    }
    Since I feel like the line having only an opening bracket is pretty much wasted. It doesn't help me see the start of the loop/block at all, since that's what the indentation is for. It doesn't help me see functions from classes, since the "class" keyword is visible enough... So I don't use it unless I have to conform to a forced/team standard.

    Bracket placement is one of those funny things... You can't really say this style is bad, and other is good (unless you compare utter chaos to a style ) - consistency is what matters more, I'd say.

  13. #13
    Join Date
    Jun 2008
    Posts
    592

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by Lindley
    Much easier to visually pick out the blocks that way.
    I agree with that . I used attached brackets when I was newer at programming, but I moved to broken brackets.
    Code:
    if( statement )
    {
    }
    
    if( statement )
    {
        // code
    }
    else if( statement )
    {
        // code
    }
    else if( statement )
    {
        // code
    }
    i like a switch statement before an else if when it is possible to use
    Code:
    switch( expression )
    {
        case expression:
        {
            // code
        }
        break;
    
        case expression:
        {
            // code
        }
        break;
    
        default:
        break;
    };
    Last edited by Joeman; September 23rd, 2010 at 10:18 PM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  14. #14
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by dshawul View Post
    I recently found myself using too many {} blocks to reclaim memory of big local variables with destructors.

    Code:
    {
          ScalarField   A ;
          do something with A;
          //destructor for A
    }  
    {
          ScalarField B;
          //destructor for B
    }
    is different from
    Code:
    ScalarField A;
    ScalarField B;
    dosomethig with A;
    dosomething with B;
    So... what is the difference, and how is one better than the other one? IMO, the only noticeable difference is less name pollution in the first case.

    Quote Originally Posted by Joeman View Post
    Code:
    switch( expression )
    {
        case expression:
        {
            // code
        }
        break;
    
        case expression:
        {
            // code
        }
        break;
    
        default:
        break;
    };
    My switches always had the break statement inside the block, or even on the same line for single statement switches, but I like your style. I think I'll adopt it.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by monarch_dodra
    So... what is the difference, and how is one better than the other one? IMO, the only noticeable difference is less name pollution in the first case.
    There is a difference in scope and thus lifetime due to the use of RAII. I think dshawul already explained the idea: to release a resource - that may have a non-negligible cost to keep around - when it is no longer in use. In a way, it is akin to the idea of a scoped lock.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured