Thread: What Do Braces Do?

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    What Do Braces Do?

    lol. umm..maybe this is a simple quetion, but i'm asking anyway.

    1)Why do you need to delimit a block of statement with curly braces? What do these curly braces do?

    2) Curly braces around data constructs such as strucs, classes, enums etc end with a semicolon, while functions have non? What's the difference? (besides the semicolon )
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Braces tell when an area of code ends. Braces aren't always required, either:
    Code:
    if(one)
    two = false;
    three = true;
    If one is true here, then two will become false and three will become true. If one is false, then two will stay the same, but three will still become true. However, this:
    Code:
    if(one) {
    two = false;
    three = true;
    }
    Will only change three if one is true.
    Do not make direct eye contact with me.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    1)Why do you need to delimit a block of statement with curly braces? What do these curly braces do?
    the curly braces around a group of statements form a statement block, which is used in the C++ grammar somewhere.

    2) Curly braces around data constructs such as strucs, classes, enums etc end with a semicolon, while functions have non? What's the difference? (besides the semicolon )
    The definitions of structs, classes, and enums are statements and all statements end with a semi-colon. The function definition is not a statement.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1)Why do you need to delimit a block of statement with curly braces? What do these curly braces do?
    There isn't much room in a cheap block, so the braces keep multiple statements from falling out.

    >Curly braces around data constructs such as strucs, classes, enums etc end with a semicolon, while functions have non?
    Functions are arrogant, thinking they can do everything. So everyone else with file scope refuses to let them have a trailing semicolon to put them in their place. It's all about suppression.

    >and all statements end with a semi-colon
    An if is a statement, so is a while loop and a for loop and a switch...
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    >Functions are arrogant, thinking they can do everything. So everyone else with file scope refuses to let them have a trailing semicolon to put them in their place. It's all about suppression.

    So what your saying is..that the compiler sees everything that doesn't have a semi-colon as something that deepens the file scope? Or manipulates the scope somehow?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Yes, you're sort of right prelude, spoke a little too fast and I've never put too much thought into it. But, alas, I think I'm right. I manage to look at someone's c-grammar on this, since I hate to be wrong, and look how it's formatted. http://www.lysator.liu.se/c/ANSI-C-g...sion-statement
    selection_statement
    : IF '(' expression ')' statement
    | IF '(' expression ')' statement ELSE statement
    | SWITCH '(' expression ')' statement
    ;

    iteration_statement
    : WHILE '(' expression ')' statement
    | DO statement WHILE '(' expression ')' ';'
    | FOR '(' expression_statement expression_statement ')' statement
    | FOR '(' expression_statement expression_statement expression ')' statement
    ;
    Last edited by okinrus; 04-17-2004 at 04:43 PM.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    A statement must follow a for loop and select statment; however, only the nonterminal compound_statement is not terminated with a ';'

  8. #8
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you don't have to follow a for loop with a statement

    Code:
    int i;
    for( i = 0; i < 50; i++);
    that line is perfectly legal. Ive used this method when iterating through an array, and wanting to stop once array[i] met the condition I wanted. i is then forced to do my bidding.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    In the case of the procedural block, the opening brace gives you
    push ebp
    mov ebp,esp

    The closing brace gives
    pop ebp
    ret
    Last edited by DarkStar; 04-18-2004 at 07:03 PM.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    you don't have to follow a for loop with a statement
    Sorry if I may have confused you. The for loop must end in a statement. The for statement itself is composed of a sequence of nonterminals, the last of which is a statement.

    Code:
    int i;
    for( i = 0; i < 50; i++);

    that line is perfectly legal. Ive used this method when iterating through an array, and wanting to stop once array[i] met the condition I wanted. i is then forced to do my bidding.
    Te grammar I showed you(the link is down for some reason went something like this for '('exprseq ';' condexpr ';'exprseq ')' stmt
    you can expand it to
    for (i = 0 ';' i < 50 ';' i++ ')' stmt
    The ending stmt is the semicolon.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you're sort of right prelude
    >But, alas, I think I'm right
    I have to assume you know what you're talking about, but what you said was wrong. Trying to fix it by claiming that my correction was incorrect is rude.

    >only the nonterminal compound_statement is not terminated with a ';'
    This is much more accurate than simply saying "all statements end with a semi-colon."
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I have to assume you know what you're talking about, but what you said was wrong.
    Most of the time. I typically have to look up arcane stuff, and at other times I rely on what others have said. Sometimes this happens to be wrong.

    Trying to fix it by claiming that my correction was incorrect is rude.
    No, I never said your correction was incorrect. "I think I'm right" is typically translated into "possibly right" but never "your wrong and I'm always right." I simply pointed out that even for loop statements are ended with a statement, intending it to be more of neat fact than to be insulting. Then I followed it by a post that only the compound_statement did not in a semi-colon, which is the only reason loops don't have to terminate in a semicolon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is curly braces here is a must?
    By mashour06 in forum C Programming
    Replies: 8
    Last Post: 04-25-2009, 04:41 PM
  2. error: braces around scalar initializer...
    By Osiris990 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2008, 03:22 PM
  3. Highlighting Braces...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-15-2005, 01:59 PM
  4. Local stacks, curly braces
    By bennyandthejets in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2003, 11:48 PM
  5. Replies: 1
    Last Post: 12-01-2001, 08:17 AM