# Thread: What Do Braces Do?

1. ## 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 )

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

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

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

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

7. A statement must follow a for loop and select statment; however, only the nonterminal compound_statement is not terminated with a ';'

8. 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. In the case of the procedural block, the opening brace gives you
push ebp
mov ebp,esp

The closing brace gives
pop ebp
ret

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

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