首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Statements

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

二次

代码语言:javascript
复制
int main()
{
    int n = 1;                        // declaration statement
    n = n + 1;                        // expression statement
    std::cout << "n = " << n << '\n'; // expression statement
    return 0;                         // return statement
}

二次

C++包括下列类型的语句:

1%29份表达陈述;

复合报表2%29份;

3%份29份甄选声明;

4%个29次迭代语句;

跳转5%份29份;

6%29份声明;

7%个29次试块;

8%29原子和同步块%28 TM TS%29。

标签

任何语句都可以贴标,在语句本身之前提供一个标签,后面跟着冒号。

attr(optional) identifier : statement

(1)

?

attr(optional) case constexpr : statement

(2)

?

attr(optional) default : statement

(3)

?

1%29目标后藤;

2%29箱标签开关声明;

3%29默认标签开关声明。

安属性序列攻击可能出现在标签%28之前--在这种情况下,它应用于标签%29,或者就在任何语句本身之前,在这种情况下,它将应用于整个语句。语句可能带有多个标签。标签%28,只有标签%29有功能范围标签被忽略不合格查找标签可以与程序中的任何其他实体具有相同的名称。

表达式语句

后面跟着分号的表达式是语句。

attr(optional) expression(optional) ;

(1)

?

attr(C++11)

-

optional sequence of any number of attributes

expression

-

an expression

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

没有表达式的表达式语句称为空语句它常用于向为或当循环。它还可以用于在复合语句的末尾携带标签。

复合语句

复合语句或砌块是用大括号括起来的语句序列。

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

(1)

?

当需要一个语句时,但需要以序列%28执行多个语句时--例如,在如果语句或循环%29,则可以使用复合语句:

二次

代码语言:javascript
复制
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

二次

每个复合语句都引入自己的块。范围;在块内声明的变量按相反顺序在结束支撑处销毁:

二次

代码语言:javascript
复制
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; 
}

二次

选择语句

SELECT语句在几个控制流中进行选择。

attr(optional) if ( condition ) statement (1) attr(optional) if ( condition ) statement else statement (2) attr(optional) switch ( condition ) statement (3)

attr(optional) if ( condition ) statement

(1)

?

attr(optional) if ( condition ) statement else statement

(2)

?

attr(optional) switch ( condition ) statement

(3)

?

(until C++17)

attr(optional) if ( condition ) statement

(1)

?

attr(optional) if ( condition ) statement else statement

(2)

?

attr(optional) switch ( condition ) statement

(3)

?

attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement (1) attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement else statement (2) attr(optional) switch ( init-statement(optional) condition ) statement (3)

attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement

(1)

?

attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement else statement

(2)

?

attr(optional) switch ( init-statement(optional) condition ) statement

(3)

?

(since C++17)

attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement

(1)

?

attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement else statement

(2)

?

attr(optional) switch ( init-statement(optional) condition ) statement

(3)

?

1%29如果声明;

2%29如果带有其他条款的陈述;

3%29开关声明。

迭代语句

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

attr(optional) while ( condition ) statement

(1)

?

attr(optional) do statement while ( expression ) ;

(2)

?

attr(optional) for ( init-statement condition(optional) ; expression(optional) ) statement

(3)

?

attr(optional) for ( for-range-decl : for-range-init ) statement

(4)

(since C++11)

1%29当循环;

2%29同时循环;

3%29为循环;

4%29范围循环。

跳转语句

跳转语句无条件传输流控制。

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)

?

1%29破断声明;

2%29继续声明;

3%29回归带有可选表达式的语句;

4%29回归语句使用列表初始化;

5%29后藤声明。

注意:对于所有跳转语句,从循环中、从块中或返回到具有自动存储持续时间的初始化变量的后面,都涉及到销毁在从传输点转移到而不是在传输到的点上的范围内的具有自动存储持续时间的对象。如果初始化多个对象,则破坏顺序与初始化顺序相反。

声明陈述

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

block-declaration ;

(1)

?

1%29见声明和初始化关于细节。

试块

try块提供了捕获在执行其他语句时引发的异常的能力。

attr(optional) try compound-statement handler-sequence

(1)

?

1%29见试/抓关于细节。

Atomic and synchronized blocks Atomic and synchronized blocks are used to implement transactional memory. 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 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.

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)

(TM TS)

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)

另见

c发言文件

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com