命名空间
变体
操作

语句

来自 cppreference.cn
< cpp‎ | language
 
 
C++ 语言
通用主题
流程控制
条件执行语句
if
迭代语句(循环)
for
范围 for (C++11)
跳转语句
函数
函数声明
Lambda 函数表达式
inline 说明符
动态异常规范 (在 C++11 中已弃用*)
noexcept 说明符 (C++11)
异常
命名空间
类型
说明符
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
存储期说明符
初始化
 
 

语句 是 C++ 程序中按顺序执行的代码片段。任何函数的主体都是一系列语句。例如

int main()
{
    int n = 1;                        // declaration statement
    n = n + 1;                        // expression statement
    std::cout << "n = " << n << '\n'; // expression statement
    return 0;                         // return statement
}

C++ 包括以下类型的语句

(自 C++26 起)
(TM TS)

内容

[编辑] 标签语句

标签语句为控制流目的标记一个语句。

label statement
label - 应用于语句的标签(定义如下)
statement - 标签应用于的语句,它可以是标签语句本身,允许多个标签

[编辑] 标签

label 定义为

attr (可选) identifier : (1)
attr (可选) case constexpr : (2)
attr (可选) default: (3)
1) goto 的目标;
2) case switch 语句中的标签;
3) default switch 语句中的标签。

属性 序列 attr 可以出现在标签的开头(在这种情况下,它应用于标签),或者在任何语句本身之前,在这种情况下,它应用于整个语句。

(自 C++11 起)

在函数内部声明的带有标识符的标签与该函数中所有嵌套块中,在其自身声明之前和之后,具有相同标识符的所有 goto 语句匹配。

函数中的两个标签不得具有相同的标识符。

除了添加到语句之外,标签还可以用于复合语句中的任何位置。

(自 C++23 起)

标签不会被非限定查找找到:标签可以与程序中的任何其他实体具有相同的名称。

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

[编辑] 控制流受限语句

以下语句是控制流受限语句

(自 C++17 起)
(自 C++23 起)

对于每个控制流受限语句 S

  • S 中声明的所有 goto 目标标签只能被 S 中的语句引用。
  • 出现在 S 中的每个 casedefault 标签只能与 S 内的 switch 语句相关联。

[编辑] 表达式语句

表达式语句是一个后跟分号的表达式。

attr (可选) expression (可选) ;
attr - (自 C++11 起) 任意数量的属性的可选序列
表达式 - 一个 表达式

典型的 C++ 程序中的大多数语句都是表达式语句,例如赋值或函数调用。

没有表达式的表达式语句称为空语句。它通常用于为 forwhile 循环提供一个空主体。 它也可以用于在复合语句的末尾携带标签。(直到 C++23)

[编辑] 复合语句

复合语句或将一系列语句组合成一个语句。

attr (可选) { statement... (可选) label... (可选)(自 C++23 起) }

当需要一个语句,但需要按顺序执行多个语句时(例如,在 if 语句或循环中),可以使用复合语句

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

每个复合语句都引入了自己的块作用域;在块内声明的变量在右大括号处以相反的顺序销毁

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

复合语句末尾的标签被视为后跟空语句。

(自 C++23 起)

[编辑] 选择语句

选择语句在多个控制流之间进行选择。

attr (可选) if constexpr(可选) ( init-statement (可选) condition ) statement (1)
attr (可选) if constexpr(可选) ( init-statement (可选) condition ) statement
    else statement
(2)
attr (可选) switch ( init-statement (可选) condition ) statement (3)
attr (可选) if !(可选) consteval compound-statement (4) (自 C++23 起)
attr (可选) if !(可选) consteval compound-statement else statement (5) (自 C++23 起)
1) if 语句;
2) 带有 else 子句的 if 语句;
3) switch 语句;
4) consteval if 语句;
5) 带有 else 子句的 consteval if 语句。

[编辑] 迭代语句

迭代语句重复执行某些代码。

attr (可选) while ( condition ) statement (1)
attr (可选) do statement while ( expression ) ; (2)
attr (可选) for ( init-statement condition (可选) ; expression (可选) ) statement (3)
attr (可选) for
    ( init-statement (可选)(自 C++20 起) for-range-decl : for-range-init ) statement
(4) (自 C++11 起)
1) while 循环;
2) do-while 循环;
3) for 循环;
4) 范围 for 循环。

[编辑] 跳转语句

跳转语句无条件地转移控制流。

attr (可选) break; (1)
attr (可选) continue; (2)
attr (可选) return expression (可选) ; (3)
attr (可选) return braced-init-list ; (4) (自 C++11 起)
attr (可选) goto identifier ; (5)
1) break 语句;
2) continue 语句;
3) 带有可选表达式的 return 语句;
4) 使用列表初始化return 语句;
5) goto 语句。

注意:对于所有跳转语句,跳出循环、块或返回到已初始化的具有自动存储期限的变量,都涉及到销毁在跳转起点处作用域内但在跳转目标处不在作用域内的具有自动存储期限的对象。如果初始化了多个对象,则销毁顺序与初始化顺序相反。

断言语句

合约断言。

contract_assert attr (可选) ( predicate ) ;
1) contract_assert 语句。
(自 C++26 起)

[编辑] 声明语句

声明语句在一个块中引入一个或多个标识符。

块声明 (1)
1) 有关详细信息,请参阅声明初始化

[编辑] try

try 块捕获执行其他语句时抛出的异常。

attr (可选) try compound-statement handler-sequence (1)
1) 有关详细信息,请参阅 try


原子和同步块

原子和同步块提供事务内存

synchronized compound-statement (1) (TM TS)
atomic_noexcept compound-statement (2) (TM TS)
atomic_cancel compound-statement (3) (TM TS)
atomic_commit compound-statement (4) (TM TS)
1) synchronized 块,与所有 synchronized 块以单一总顺序执行;
2) atomic 块,在异常时中止;
3) atomic 块,在异常时回滚;
4) atomic 块,在异常时提交。
(TM TS)

[编辑] 子语句

语句的子语句是以下之一

如果满足以下任何条件,则语句 S1 包围 语句 S2

  • S2S1 的子语句
  • S1 是选择语句或迭代语句,并且 S2S1init-statement
  • S1try,并且 S2 是其 compound-statement 或其 handler-seq 中任何 handlercompound-statement
  • S1 包围语句 S3S3 包围 S2

如果 S2 包围 S1,则语句 S1 语句 S2 包围

[编辑] 参见

C 文档 关于 语句