4.3 Compound Statements
A
compound statement is a sequence of
zero or more statements enclosed within curly braces. Compound
statements are frequently used in selection and
loop statements. They
enable you to write loop bodies that are more than one statement
long, among other things. A compound statement is sometimes called a
block.
Here is the syntax of a compound statement:
{ statement ... }
or:
{ }
A compound statement can be used as a single statement wherever a
statement is called for. A compound statement delimits a declarative
scope, that is, any name declared in the compound statement is not
visible outside the statement, and names in the statement can hide
names from outside the statement. The lifetime of any automatic
object declared in a compound statement is confined to that
statement. All such objects are destroyed when execution leaves the
compound statement for any reason (e.g., branching outside the
statement, execution reaches the end of the statement, or an
exception is thrown).
Compound statements are most often used as the bodies of selection
and loop statements, but you can also stick a compound statement in
the middle of a compound statement to restrict the scope of variables
that are local to the compound statement. See the examples in this
chapter for uses of compound statements.
|