braces are the C/C++ equivalent to BEGIN/END in other languages.
Only switch and try statements dictate braces ( i think), this is because switch() contains multiple statements, and try catch reflects standard exception processing that will unwind a stack (the same thing as closing a scope)
All other statements (i think) affect only the statement that follows it.
eg;
1 2
|
while (true)
DoSomething();
|
if you want to execute more than one statement you need to use braces to wrap them up into a single "compound" statement code
1 2 3 4 5
|
while (true)
{
DoSomething();
DoSomethingElse();
}
|
Braces are also used heavily in declarations, classes, functions, structs, enums etc...
http://www.cplusplus.com/doc/tutorial/control/
@mBozzi, lazpeng is correct when saying
variables declared inside a specific scope are deallocated when they go out of the scope |
the data you describe is not declared, it is allocated at run time. The pointer is declared, so is deallocated. I always consider "declared" to mean "declared to the compiler so it can manage its lifetime".
Also switch statements are not consistent with other C/C++ statements, they are a hang up from earlier languages like BCPL. this is why the options are declared with goto labels
case label:
instead of blocks, its also why break is required for each option, it's a standin for
goto endofswitch