1. Statements and Blocks
|
|
1.1 Statements |
|
Braces { and } are used to group declarations and statements together into a compound statement, or block, so that they are syntactically equivalent to a single statement. |
A simple statement ends in a semicolon: z = foo(x+y); |
Consider the multiple statements: |
temp = x+y;
z = foo(temp); |
|
Curly braces – combine into compound statement/block |
|
|
1.2 Blocks |
|
Blocks |
– Block can substitute for simple statement.
– Compiled as a single unit
– Variables can be declared inside |
{
int temp = x+y;
z = foo(temp);
} |
|
– Block can be empty {} and No semicolon at end. |
Nested Blocks |
– Blocks nested inside each other |
{
int temp = x+y;
z = foo(temp);
{
float temp2 = x∗y;
z += bar(temp2);
}
} |
|
|
|
|
|