Difference between revisions of "cpp/language/statements"
(statement types in italic, improved formatting of the comments, "loop" -> "statement" like selection and jump statements, "cout" -> "std::cout") |
m (rv return statement) |
||
Line 8: | Line 8: | ||
n = n + 1; // expression statement | n = n + 1; // expression statement | ||
std::cout << "n = " << n << '\n'; // expression statement | std::cout << "n = " << n << '\n'; // expression statement | ||
− | return 0; // | + | return 0; // return statement |
} | } | ||
}} | }} |
Revision as of 06:49, 21 January 2016
Statements are fragments of the C++ program that are executed in sequence. The body of any function is a sequence of statements. For example:
int main() { int n = 1; // declaration statement n = n + 1; // expression statement std::cout << "n = " << n << '\n'; // expression statement return 0; // return statement }
There are seven types of statements:
Contents |
Labels
A labeled statement is a label followed by a colon before the statement itself.
attr(optional) identifier : statement
|
(1) | ||||||||
attr(optional) case constexpr : statement
|
(2) | ||||||||
attr(optional) default : statement
|
(3) | ||||||||
An attribute sequence attr may appear just before the label (in which case it applies to the label), or just before any statement itself, in which case it applies to the entire statement. A statement may carry multiple labels. Labels (and only labels) have function scope. Labels are ignored by unqualified lookup: a label can have the same name as any other entity in the program.
Expression statements
An expression statement is an expression followed by a semicolon.
attr(optional) expression(optional) ;
|
(1) | ||||||||
attr(C++11) | - | optional sequence of any number of attributes |
expression | - | an expression |
Most statements in a typical C++ program are expression statements, such as assignments or function calls.
An expression statement without an expression is called a null statement. It is often used to provide an empty body to a for or while statement. It can also be used to carry a label in the end of a compound statement.
Compound statements
A compound statement or block is a brace-enclosed sequence of statements.
attr(optional) { statement...(optional) }
|
(1) | ||||||||
When one statement is expected, but multiple statements need to be executed in sequence (for example, in an if statement or an iteration statement), a compound statement may be used:
if (x > 5) // start of if statement { // start of compound statement int n = 1; // declaration statement std::cout << n; // expression statement } // end of compound statement, end of if statement
Each compound statement introduces its own block scope: variables declared inside a block are destroyed at the closing brace in reverse order of declaration:
int main() { { // start of compound statement std::ofstream f("test.txt"); // declaration statment f << "abc\n"; // expression statement } // end of compound statement, // f is flushed and closed by ofstream's destructor std::ifstream f("test.txt"); std::string str; f >> str; }
Selection statements
A selection statement chooses between one of several flows of control.
attr(optional) if ( condition ) statement
|
(1) | ||||||||
attr(optional) if ( condition ) statement else statement
|
(2) | ||||||||
attr(optional) switch ( condition ) statement
|
(3) | ||||||||
Iteration statements
An iteration statement repeatedly executes some code.
attr(optional) while ( condition ) statement
|
(1) | ||||||||
attr(optional) do statement while ( expression ) ;
|
(2) | ||||||||
attr(optional) for ( for-init-statement condition(optional) ; expression(optional) ) statement
|
(3) | ||||||||
attr(optional) for ( for-range-decl : for-range-init ) statement
|
(4) | (since C++11) | |||||||
Jump statements
A jump statement unconditionally transfers flow of control.
attr(optional) break ;
|
(1) | ||||||||
attr(optional) continue ;
|
(2) | ||||||||
attr(optional) return expression(optional) ;
|
(3) | ||||||||
attr(optional) return braced-init-list ;
|
(4) | (since C++11) | |||||||
attr(optional) goto identifier ;
|
(5) | ||||||||
Note: for all jump statements, transfer out of an iteration statement, out of a block, or back past an initialized variable with automatic storage duration involves the destruction of objects with automatic storage duration that are in scope at the point transferred from but not at the point transferred to. If multiple objects were initialized, the order of destruction is the opposite of the order of initialization.
Declaration statements
A declaration statement introduces one or more identifiers into a block.
block-declaration ;
|
(1) | ||||||||
Try blocks
A try block provides the ability to catch exceptions thrown when executing other statements.
attr(optional) try compound-statement handler-sequence
|
(1) | ||||||||
See also
C documentation for Statements
|