Statements define and control what a program does. This chapter describes the syntax and rules for C++ statements: expressions, loops, selection, and control. The statement syntax rules apply recursively, and wherever a statement is called for, you can use (almost) any of the statements in this chapter.
The syntax descriptions in this chapter are informal. See Chapter 12 for a precise BNF grammar.
An expression statement computes an expression, such as a function call or assignment. The expression result is discarded, so the expression is typically evaluated for its side effects. (See Chapter 3 for details about expressions.) The statement syntax is simply an optional expression followed by a semicolon:
expr
;
or:
;
A statement with no expression is called a null statement . Null statements are most often used for loops when no code is needed in the loop body.
Here are several examples of expression statements:
42; // Valid but pointless cout << 42; // More typical x = y * z; // Remember that assignment is an expression ; // Null statement