Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Statements

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
       

      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 statementstd::cout<<"n = "<< n<<'\n';// expression statementreturn0;// return statement}

      C++ includes the following types of statements:

      (since C++26)
      (TM TS)

      Contents

      [edit]Labeled statements

      A labeled statement labels a statement for control flow purposes.

      label statement
      label - the label applied to the statement (defined below)
      statement - the statement which the label applies to, it can be a labeled statement itself, allowing multiple labels

      [edit]Labels

      label is defined as

      attr (optional)identifier: (1)
      attr (optional)caseconstexpr: (2)
      attr (optional)default: (3)
      1) target forgoto;
      2)case label in aswitch statement;
      3)default label in aswitch statement.

      Anattribute sequenceattr may appear just at the beginning of 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.

      (since C++11)

      A label with an identifier declared inside a function matches all goto statements with the same identifier in that function, in all nested blocks, before and after its own declaration.

      Two labels in a function must not have the same identifier.

      Besides being added to a statement, labels can also be used anywhere incompound statements.

      (since C++23)

      Labels are not found byunqualified lookup: a label can have the same name as any other entity in the program.

      void f(){{goto label;// label in scope even though declared later        label:// label can appear at the end of a block standalone since C++23}goto label;// label ignores block scope} void g(){goto label;// error: label not in scope in g()}

      [edit]Control-flow-limited statements

      The following statements arecontrol-flow-limited statements :

      (since C++17)
      (since C++23)

      For each control-flow-limited statementS:

      • Allgoto target labels delcared inS can only be referred to by statements inS.
      • Eachcase ordefault label appearing withinS can only be associated with aswitch statement withinS.

      [edit]Expression statements

      An expression statement is an expression followed by a semicolon.

      attr (optional)expression (optional);
      attr -(since C++11) optional sequence of any number ofattributes
      expression - anexpression

      Most statements in a typical C++ program are expression statements, such as assignments or function calls.

      An expression statement without an expression is called anull statement. It is often used to provide an empty body to afor orwhile loop.It can also be used to carry a label in the end of a compound statement.(until C++23)

      [edit]Compound statements

      A compound statement orblock groups a sequence of statements into a single statement.

      attr (optional){statement... (optional)label... (optional)(since C++23)}

      When one statement is expected, but multiple statements need to be executed in sequence (for example, in anif statement or a loop), a compound statement may be used:

      if(x>5)// start of if statement{// start of blockint n=1;// declaration statementstd::cout<< n;// expression statement}// end of block, end of if statement

      Each compound statement introduces its own blockscope; variables declared inside a block are destroyed at the closing brace in reverse order:

      int main(){// start of outer block{// start of inner blockstd::ofstream f("test.txt");// declaration statement        f<<"abc\n";// expression statement}// end of inner block, f is flushed and closedstd::ifstream f("test.txt");// declaration statementstd::string str;// declaration statement    f>> str;// expression statement}// end of outer block, str is destroyed, f is closed

      Alabel at the end of a compound statement is treated as if it were followed by a null statement.

      (since C++23)

      [edit]Selection statements

      A selection statement chooses between one of several control flows.

      attr (optional)if constexpr(optional)(init-statement (optional)condition)statement (1)
      attr (optional)if constexpr(optional)(init-statement (optional)condition)statement
          elsestatement
      (2)
      attr (optional)switch (init-statement (optional)condition)statement (3)
      attr (optional)if !(optional)constevalcompound-statement (4)(since C++23)
      attr (optional)if !(optional)constevalcompound-statementelsestatement (5)(since C++23)
      1)if statement;
      2)if statement with an else clause;
      3)switch statement;
      4)consteval if statement;
      5)consteval if statement with an else clause.

      [edit]Iteration statements

      An iteration statement repeatedly executes some code.

      attr (optional)while (condition)statement (1)
      attr (optional)dostatementwhile (expression); (2)
      attr (optional)for (init-statement condition (optional);expression (optional))statement (3)
      attr (optional)for
          (init-statement (optional)(since C++20)for-range-decl:for-range-init)statement
      (4)(since C++11)
      1)while loop;
      2)do-while loop;
      3)for loop;
      4)range for loop.

      [edit]Jump statements

      A jump statement unconditionally transfers control flow.

      attr (optional)break; (1)
      attr (optional)continue; (2)
      attr (optional)returnexpression (optional); (3)
      attr (optional)returnbraced-init-list; (4)(since C++11)
      attr (optional)gotoidentifier; (5)
      1)break statement;
      2)continue statement;
      3)return statement with an optional expression;
      4)return statement usinglist initialization;
      5)goto statement.

      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.

      Assertion statements

      A contract assertion.

      contract_assertattr (optional)(predicate);
      1)contract_assert statement.
      (since C++26)

      [edit]Declaration statements

      A declaration statement introduces one or more identifiers into a block.

      block-declaration (1)
      1) SeeDeclarations andInitialization for details.

      [edit]try blocks

      Atry block catches exceptions thrown when executing other statements.

      attr (optional)trycompound-statement handler-sequence (1)
      1) Seetry block for details.


      Atomic and synchronized blocks

      An atomic and synchronized block providestransactional memory.

      synchronizedcompound-statement (1)(TM TS)
      atomic_noexceptcompound-statement (2)(TM TS)
      atomic_cancelcompound-statement (3)(TM TS)
      atomic_commitcompound-statement (4)(TM TS)
      1)synchronized block, executed in single total order with all synchronized blocks;
      2)atomic block that aborts on exceptions;
      3)atomic block that rolls back on exceptions;
      4)atomic block that commits on exceptions.
      (TM TS)

      [edit]Substatements

      Asubstatement of a statement is one of the following:

      A statementS1encloses a statementS2 if any of the following conditions is satisfied:

      • S2 is a substatement ofS1
      • S1 is a selection statement or iteration statement, andS2 is theinit-statement ofS1.
      • S1 is atry block, andS2 is either itscompound-statement or thecompound-statement of anyhandler in itshandler-seq .
      • S1 encloses a statementS3 andS3 enclosesS2.

      A statementS1 isenclosed by a statementS2 ifS2 enclosesS1.

      [edit]See also

      C documentation forStatements
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/statements&oldid=181015"

      [8]ページ先頭

      ©2009-2025 Movatter.jp