Previous | Table of Contents | Next |
Like most programming languages, C++ distinguishes between declarations, expressions, and statements. In appropriate contexts, declarations and statements can be nested within other declarations and statements, but neither can be nested inside an expression. Every statement ultimately appears inside the definition of a function, where it forms part of what happens when that function is called.
Unless otherwise specified, the statements that constitute a function are executed in the order in which they appear. Exceptions include: loops; calls to functions; the goto, break, and continue statements; and the try and throw statements associated with exception handling.
Statements are written in free form, in the sense that beginning a new line in mid-statement does not affect the statements meaning. Accordingly, most statements end with semicolons, the main exception being the block (which begins with { and ends with }).
7.2.9.1. The Null Statement
The simplest statement is the null statement, which is just a semicolon. We have already encountered a common use for a null statement, in this context:
int strlen(const char* p) { const char* q = p; while (*q++ != \0) { } return q - p - 1; }
Here, the loop
while (*q++ != \0) { }
could just as well have been written
while (*q++ != \0);
where the semicolon is a null statement. However, that semicolon is so inconspicuous that it would be better to write
while (*q++ != \0) ;
or even
while (*q++ != \0) ;
to emphasize that there is something there.
7.2.9.2. The Expression Statement
Placing a semicolon after any expression turns it into a statement, which, when executed, evaluates the expression and discards the result. For that reason, most expression statements have useful side effects, either assigning a value to a variable, as in
x = 42;
or calling a function, as in
cout << Hello, world! << endl;
In this latter example, the function called is the second << operator; its arguments are the subexpression
cout << Hello, world!
and the object endl.
7.2.9.3. Blocks
Enclosing zero or more statements in curly braces { } turns them into a single statement. Declarations can be intermixed with those statements; such declarations define local variables that persist (only) until the }. Aside from defining local variables, the most common use of blocks is as the subject of an if or while statement. For example, if x and y are variables of type int, we might write
if (x < y) { int z = x; x = y; y = z; }
to exchange x and y if x<y.
7.2.9.4. Conditionals
The C++ conditional statement takes one of the forms
if (expression) statement if (expression) statement else statement
Note that expression must be surrounded by parentheses, and that then is not a keyword in C++. As with other languages with similar conditional statements, the ambiguity in
if (expression1) if (expression2) statement1 else statement2
is resolved by associating each else with the nearest unmatched if, so that this example is equivalent to
if (expression1) { if (expression2) statement1 else statement2 }
The expression in an if statement, as in other conditional contexts, is considered to be false if it evaluates to zero and true otherwise. It is common to use values of nonnumeric types, such as pointers, in if statements. For example, you might write
if (p) *p = 0;
instead of
if (p != 0) *p = 0;
7.2.9.5. Loops
The fundamental way to write a loop in C++ is the while statement, which is syntactically similar to the if statement:
while (expression) statement
To write a loop that must always be executed at least once, use the form
do statement while (expression);
tests the value of the expression at the end of the loop instead of at the beginning.
In addition, the common form
{ statement1 while (expression) { statement2 expression2; } }
can be abbreviated as
for(statement1 expression; expression2) statement2
There is no semicolon between statement1 and expression because statement1 will usually end with a semicolon. So, for example, if a is an array with 100 elements, we can set those elements to zero by writing
{ int i = 0; while (i < 100) { a[i] = 0; ++i; } }
where the outermost { } are there to bound the scope of the local variable i. We can achieve the same effect by writing
for (int i = 0; i < 100; ++i) a[i] = 0;
Similarly, we can turn
i = 0; j = 1; while (i < 100) { a[i] = j; ++i; j += 7; }
into
for ({ i = 0; j = 1; } i < 100; ++i, j += 7) a[i] = j;
by using the comma operator to combine the two expression statements
++i; j += 7;
into the single expression statement
++i, j += 7;
and then using its constituent expression in the for statement. Note that there is no semicolon after the }.
Previous | Table of Contents | Next |