Difference between revisions of "cpp/language/statements"
m (rv return statement) |
(loop and block were correct. plural was correct) |
||
Line 2: | Line 2: | ||
{{cpp/language/statements/navbar}} | {{cpp/language/statements/navbar}} | ||
Statements are fragments of the C++ program that are executed in sequence. The body of any function is a sequence of statements. For example: | Statements are fragments of the C++ program that are executed in sequence. The body of any function is a sequence of statements. For example: | ||
+ | |||
{{source|1= | {{source|1= | ||
int main() | int main() | ||
Line 11: | Line 12: | ||
} | } | ||
}} | }} | ||
+ | |||
There are seven types of statements: | There are seven types of statements: | ||
− | @1@ expression | + | |
− | @2@ compound | + | @1@ expression statements; |
− | @3@ selection | + | @2@ compound statements; |
− | @4@ iteration | + | @3@ selection statements; |
− | @5@ jump | + | @4@ iteration statements; |
− | @6@ declaration | + | @5@ jump statements; |
− | @7@ try | + | @6@ declaration statements; |
+ | @7@ try blocks. | ||
===Labels=== | ===Labels=== | ||
− | + | Any statement can be ''labeled'', by providing a label followed by a colon before the statement itself: | |
+ | |||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
Line 34: | Line 38: | ||
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ target for {{rlp|goto}} | + | @1@ target for {{rlp|goto}}. |
− | @2@ case label in a {{rlp|switch}} statement | + | @2@ case label in a {{rlp|switch}} statement. |
@3@ default label in a {{rlp|switch}} statement. | @3@ default label in a {{rlp|switch}} statement. | ||
Line 41: | Line 45: | ||
===Expression statements=== | ===Expression statements=== | ||
− | An | + | An expression followed by a semicolon is a statement. |
+ | |||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
Line 55: | Line 60: | ||
Most statements in a typical C++ program are expression statements, such as assignments or function calls. | 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 {{rlp|for}} or {{rlp|while}} | + | An expression statement without an expression is called a ''null statement''. It is often used to provide an empty body to a {{rlp|for}} or {{rlp|while}} loop. It can also be used to carry a label in the end of a compound statement. |
+ | |||
+ | ===Compound statement=== | ||
+ | A compound statement or ''block'' is a brace-enclosed sequence of statements. | ||
− | |||
− | |||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
Line 65: | Line 71: | ||
{{sdsc end}} | {{sdsc end}} | ||
− | When one statement is expected, but multiple statements need to be executed in sequence (for example, in an {{rlp|if}} statement or | + | When one statement is expected, but multiple statements need to be executed in sequence (for example, in an {{rlp|if}} statement or a loop), a compound statement may be used: |
+ | |||
{{source|1= | {{source|1= | ||
if (x > 5) // start of if statement | if (x > 5) // start of if statement | ||
− | { // start of | + | { // start of block |
int n = 1; // declaration statement | int n = 1; // declaration statement | ||
std::cout << n; // expression statement | std::cout << n; // expression statement | ||
− | } // end of | + | } // end of block, end of if statement |
}} | }} | ||
− | Each compound statement introduces its own block {{rlp|scope}}: variables declared inside a block are destroyed at the closing brace in reverse order | + | Each compound statement introduces its own block {{rlp|scope}}: variables declared inside a block are destroyed at the closing brace in reverse order. |
+ | |||
{{source|1= | {{source|1= | ||
int main() | int main() | ||
{ | { | ||
− | { // start of | + | { // start of block |
std::ofstream f("test.txt"); // declaration statment | std::ofstream f("test.txt"); // declaration statment | ||
f << "abc\n"; // expression statement | f << "abc\n"; // expression statement | ||
− | } // end of | + | } // end of block: f is flushed and closed |
− | + | std::ifstream f("test.txt"); | |
− | std::ifstream f("test.txt"); | + | |
std::string str; | std::string str; | ||
− | f >> str; | + | f >> str; |
} | } | ||
}} | }} | ||
===Selection statements=== | ===Selection statements=== | ||
− | + | Selection statements choose between one of several flows of control | |
+ | |||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
Line 108: | Line 116: | ||
===Iteration statements=== | ===Iteration statements=== | ||
− | + | Iteration statements repeatedly execute some code. | |
+ | |||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
Line 124: | Line 133: | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ {{rlp|while}} | + | @1@ {{rlp|while}} loop; |
− | @2@ {{rlp|do|do-while}} | + | @2@ {{rlp|do|do-while}} loop; |
− | @3@ {{rlp|for|for}} | + | @3@ {{rlp|for|for}} loop; |
− | @4@ {{rlp|range-for|range for}} | + | @4@ {{rlp|range-for|range for}} loop. |
===Jump statements=== | ===Jump statements=== | ||
− | + | Jump statements unconditionally transfer flow control | |
+ | |||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
Line 155: | Line 165: | ||
@5@ {{rlp|goto}} statement. | @5@ {{rlp|goto}} statement. | ||
− | Note: for all jump statements, transfer out of | + | Note: for all jump statements, transfer out of a loop, 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=== | ===Declaration statements=== | ||
− | + | Declaration statements introduce one or more identifiers into a block. | |
+ | |||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
Line 167: | Line 178: | ||
===Try blocks=== | ===Try blocks=== | ||
− | + | Try blocks provide the ability to catch exceptions thrown when executing other statements. | |
+ | |||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= |
Revision as of 07:46, 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
Any statement can be labeled, by providing 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 followed by a semicolon is a statement.
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 loop. It can also be used to carry a label in the end of a compound statement.
Compound statement
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 a loop), a compound statement may be used:
if (x > 5) // start of if statement { // start of block int n = 1; // declaration statement std::cout << n; // expression statement } // end of block, 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.
int main() { { // start of block std::ofstream f("test.txt"); // declaration statment f << "abc\n"; // expression statement } // end of block: f is flushed and closed std::ifstream f("test.txt"); std::string str; f >> str; }
Selection statements
Selection statements choose 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
Iteration statements repeatedly execute 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
Jump statements unconditionally transfer flow 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 a loop, 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
Declaration statements introduce one or more identifiers into a block.
block-declaration ;
|
(1) | ||||||||
Try blocks
Try blocks provide the ability to catch exceptions thrown when executing other statements.
attr(optional) try compound-statement handler-sequence
|
(1) | ||||||||
See also
C documentation for Statements
|