Statements are fragments of the C++ program that are executed in s equence. The body of any function
is a s equence of s tatements . 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
}
C++ includes the following types of s tatements :
1) expres s ion s tatements ;
2) compound s tatements ;
3) s election s tatements ;
4) iteration s tatements ;
5) jump s tatements ;
6) declaration s tatements ;
7) try blocks ;
8) atomic and s ynchronized blocks (TM TS ) .
Labels
Any s tatement can be labeled, by providing a label followed by a colon before the s tatement its elf.
(1)
(2)
(3)
Expression statements
An expres s ion followed by a s emicolon is a s tatement.
(1)
Compound statements
Compound s tatements or blocks are brace-enclos ed s equences of s tatements .
(1)
When one s tatement is expected, but multiple s tatements need to be executed in s equence (for example,
in an if s tatement or a loop), a compound s tatement may be us ed:
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 s tatement introduces its own block s cope; variables declared ins ide a block are
des troyed at the clos ing brace in revers e 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 s tatements choos e between one of s everal ows of control.
(1)
(2)
(3)
1) if s tatement;
2) if s tatement with an els e claus e;
3) s witch s tatement.
Iteration statements
Iteration s tatements repeatedly execute s ome code.
(1)
(2)
(3)
(4)
1) while loop;
2) do-while loop;
3) for loop;
4) range for loop.
Jump statements
Jump s tatements unconditionally trans fer ow control
(sinc e
C++11)
(1)
(2)
(3)
(4)
(5)
(sinc e C++11)
1) break s tatement;
2) continue s tatement;
3) return s tatement with an optional expres s ion;
4) return s tatement us ing lis t initialization;
5) goto s tatement.
Note: for all jump s tatements , trans fer out of a loop, out of a block, or back pas t an initialized variable
with automatic s torage duration involves the des truction of objects with automatic s torage duration that
are in s cope at the point trans ferred from but not at the point trans ferred to. If multiple objects were
initialized, the order of des truction is the oppos ite of the order of initialization.
Declaration statements
Declaration s tatements introduce one or more identiers into a block.
block-declaration ;
(1)
Try blocks
Try blocks provide the ability to catch exceptions thrown when executing other s tatements .
(1)
(1)
(TM TS )
atomic_noexceptcompound-statement
(2)
(TM TS )
atomic_cancelcompound-statement
(3)
(TM TS )
atomic_commitcompound-statement
(4)
(TM TS )
(TM TS )
1) s ynchronized block, executed in s ingle total order with all s ynchronized blocks ;
2) atomic block that aborts on exceptions ;
3) atomic block that rolls back on exceptions ;
4) atomic block that commits on exceptions .
See also
C documentation for Statements
Retrieved from "http://en.c ppreferenc e.c om/mwiki/index.php?title=c pp/language/statements& oldid=83123"