You are on page 1of 3

Statements

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.

attr (optional) identier :statement

(1)

attr (optional) caseconstexpr :statement

(2)

attr (optional) default:statement

(3)

1) target for goto;


2) cas e label in a s witch s tatement;
3) default label in a s witch s tatement.
An attribute s equence attr may appear jus t before the label (in which cas e it applies to the label), or jus t
before any s tatement its elf, in which cas e it applies to the entire s tatement. A s tatement may carry
multiple labels . Labels (and only labels ) have function s cope. Labels are ignored by unqualied lookup: a
label can have the s ame name as any other entity in the program.

Expression statements
An expres s ion followed by a s emicolon is a s tatement.

attr (optional) expression(optional) ;

(1)

attr(C++11) - optional s equence of any number of attributes


expression - an expres s ion
Mos t s tatements in a typical C++ program are expres s ion s tatements , s uch as as s ignments or function
calls .
An expres s ion s tatement without an expres s ion is called a null statement. It is often us ed to provide an
empty body to a for or while loop. It can als o be us ed to carry a label in the end of a compound
s tatement.

Compound statements
Compound s tatements or blocks are brace-enclos ed s equences of s tatements .

attr (optional) {statement...(optional) }

(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.

attr (optional) if(condition )statement

(1)

attr (optional) if(condition )statement elsestatement

(2)

attr (optional) switch(condition )statement

(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.

attr (optional) while(condition )statement

(1)

attr (optional) dostatement 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)

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)

attr (optional) break;

(1)

attr (optional) continue;

(2)

attr (optional) returnexpression(optional) ;

(3)

attr (optional) returnbraced-init-list ;

(4)

attr (optional) gotoidentier ;

(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)

1) s ee Declarations and Initialization for details .

Try blocks
Try blocks provide the ability to catch exceptions thrown when executing other s tatements .

attr (optional) trycompound-statement handler-sequence

(1)

1) s ee try/catch for details .

Atomic and synchronized blocks


Atomic and s ynchronized blocks are us ed to implement trans actional memory.
synchronizedcompound-statement

(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"