Compound statement

As in ANSI C, the compound statement is a major feature in C++, but it has removed the distinction between declarations and statements. The only restriction is that a variable must be declared before it is used.

AS in the ANSI C notes, we use an extended form of BNF notation to describe the syntax of our language. If you are not familiar with the format we use see our definition of EBNF.

compound-statement
		{ statement-list ? }

statement-list
		statement
		statement-list statement

statement
		declaration-statement
		expression-statement
		function-statement
		flow-control-statement
		compound-statement
A declaration-statement is any declaration of a variable. As in C, variables can be declared along with an initialsation. In C++ this can include a value computed in earlier statements in that block. In C this is impossible, as all declarations come at the beginning of the block.

An expression-statement is an expression, including assignments.

A function-statement is a function call.

A flow-control statement is a conditional, switch or loop statement, as in C.

Example 2

int main() {
   int j = 4;
   j++;
   int k = ++j;
   return 0;
}

Next note in series

Back to index