is curly braces here is a must?

This is a discussion on is curly braces here is a must? within the C Programming forums, part of the General Programming Boards category; Hello, is the following: Code: for ( i = 1 ; i < 5 ; i++) if (x == 1) ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    is curly braces here is a must?

    Hello,
    is the following:

    Code:
    for ( i = 1 ; i < 5 ; i++)
          if (x == 1)
              y = 5 * x;
    (Without curly braces)

    the same as:
    Code:
    for ( i = 1 ; i < 5 ; i++)
          {
                 if (x == 1)
                     y = 5 * x;
          }
    (With curly braces)?

    Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes.

    The reason is that the if(condition) statement line of code, is not a full line, we write it that way to help us, but the compiler see's it as ONE statement, until it reaches the ending semi-colon.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Did you try compiling it and seeing what happens? That is where I would start if I were you.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Normally I put curly braces around all blocks just for readability and so that I do not have to guess what the code is going to do because of some misplaced or absent curlies.
    Code:
    for ( i = 1 ; i < 5 ; i++)
    {
          if (x == 1)
          {
              y = 5 * x;
          }
    }
    The code as it is makes no sense though.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    147
    Huge agreement with Bubba

    ...and for another reason...debugging.

    If you use a GUI debugger and want to set breakpoints, it's nearly impossible to get them write unless you have a coding style like Bubba's.

    Even:

    if ( a ) c = d;

    Is a problem in this regard, because you can't set a breakpoint on c=d that fires only when "a" is true, and this comes up enough that it's worth the brackets, IMO.

    Another reason to use the brackets - expansion.

    Today, it's just c=d, then that turns into c=d, g=f, then you need to introduce a function call, and soon you have to add the brackets anyway.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes because it's so hard to go back later and add a pair of braces around a statement that doesn't have them.

    I also prefer braces, but those were some of the lamest reasons I've ever read.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,146
    For the original question, the answer, as pointed out, is they are equivalent.

    I suggest putting curly braces around everything, too, though.

    Suppose you have something like
    Code:
    for ( i = 1 ; i < 5 ; i++)
          y += i;
    And later decided to add one more statement to the loop, if you are not careful, you can be misled by the indentation.

    Code:
    for ( i = 1 ; i < 5 ; i++)
          y += i;
          x += i;
    That, however, is equivalent to
    Code:
    for ( i = 1 ; i < 5 ; i++) {
          y += i;
    }
    x += i;
    The compiler won't necessarily complain because the code can be and often is perfectly legal.

    This can lead to some hard to find bugs.

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    I also prefer braces, but those were some of the lamest reasons I've ever read.
    How about I add another one saying that the coding standards at work require I put curlies around everything so I have gotten used to doing it this way? Is that a better reason?

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    Thank you

    Thank you all for your support.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-28-2008, 10:51 PM
  2. error: braces around scalar initializer...
    By Osiris990 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2008, 02:22 PM
  3. Question (about missing curly braces)
    By Dag_ in forum C++ Programming
    Replies: 12
    Last Post: 02-18-2005, 03:57 PM
  4. What Do Braces Do?
    By jrahhali in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2004, 04:14 PM
  5. Local stacks, curly braces
    By bennyandthejets in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2003, 11:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21