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

for loop

执行init-语句一次,然后执行语句和迭代。[医]表达式,直到条件的值变为假为止。测试在每次迭代之前进行。

句法

formal syntax:

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

informal syntax:

attr(optional) for ( declaration-or-expression(optional) ; declaration-or-expression(optional) ; expression(optional) ) statement

attr(C++11)

-

any number of attributes

init-statement

-

either an expression statement (which may be a null statement ";") a simple declaration, typically a declaration of a loop counter variable with initializer, but it may declare arbitrary many variables Note that any init-statement must end with a semicolon ;, which is why it is often described informally as an expression or a declaration followed by a semicolon.

condition

-

either an expression which is contextually convertible to bool. This expression is evaluated before each iteration, and if it yields false, the loop is exited. a declaration of a single variable with a brace-or-equals initializer. the initializer is evaluated before each iteration, and if the value of the declared variable converts to false, the loop is exited.

iteration_expression

-

any expression, which is executed after every iteration of the loop and before re-evaluating condition. Typically, this is the expression that increments the loop counter

statement

-

any statement, typically a compound statement, which is the body of the loop

  • 安表达式语句%28,这可能是空语句;“%29
  • 阿简单声明,通常是带有初始化器的循环计数器变量的声明,但它可以声明任意多个变量。

注意,任何init语句都必须以分号结尾。;,这就是为什么它经常被非正式地描述为一个表达式或一个声明,后面跟着分号。条件-或者

  • 安表达那就是上下文可转换敬嘘声。此表达式在每次迭代之前进行计算,如果它生成false,循环退出。
  • 阿声明具有大括号或等号的单个变量的初始化器初始化器在每次迭代之前进行计算,如果声明变量的值转换为false,循环退出。
代码语言:txt
复制
 iteration\_expression   -   any [expression](expressions), which is executed after every iteration of the loop and before re-evaluating condition. Typically, this is the expression that increments the loop counter     statement   -   any [statement](statements), typically a compound statement, which is the body of the loop   

解释

上述语法生成的代码相当于:

{ init_statement while ( condition ) { statement iteration_expression ; } }

除了这个。

1%init语句%28 if init-语句声明的29个名称为声明%29,条件%28声明的名称为声明%29,而由条件%28声明的名称位于相同的范围%28中,这也是语句%29的范围。

2%29继续在语句中将执行迭代。[医]表达

3%29空条件相当于while(true)

如果需要在某个点终止循环的执行,断续语句可用作终止语句。

如果循环的执行需要在循环主体的末尾继续,继续语句可以用作快捷方式。

就像当循环,如果语句是单个语句%28,而不是复合语句%29,则其中声明的变量范围仅限于循环体,就好像它是复合语句一样。

二次

代码语言:javascript
复制
for (;;)
    int n;
// n goes out of scope

二次

关键词

for...

注记

作为C++的一部分前进保障,行为是未定如果循环没有可观察行为%28不调用I/O函数、访问易失性对象或执行原子或同步操作%29不终止。编译器可以删除这些循环。

在C++中,init语句的作用域和语句的范围是相同的,而在C中,语句的作用域嵌套在init语句的范围内:

二次

代码语言:javascript
复制
for (int i = 0; ; ) {
    long i = 1;   // valid C, invalid C++
    // ...
}

二次

二次

代码语言:javascript
复制
#include <iostream>
#include <vector>
 
int main()
{
    // typical loop with a single statement as the body
    for (int i = 0; i < 10; ++i)
        std::cout << i << ' ';
    std::cout << '\n';
 
    // init-statement can declare multiple names, as long as they
    // can use the same decl-specifier-seq
    for (int i = 0, *p = &i; i < 9; i += 2) {
        std::cout << i << ':' << *p << ' ';
    }
    std::cout << '\n';
 
    // condition may be a declaration
    char cstr[] = "Hello";
    for (int n = 0; char c = cstr[n]; ++n)
        std::cout << c;
    std::cout << '\n';
 
    // init-statement can use the auto type specifier
    std::vector<int> v = {3, 1, 4, 1, 5, 9};
    for (auto iter = v.begin(); iter != v.end(); ++iter) {
        std::cout << *iter << ' ';
    }
    std::cout << '\n';
 
   // init-statement can be an expression
    int n = 0;
    for (std::cout << "Loop start\n";
         std::cout << "Loop test\n";
         std::cout << "Iteration " << ++n << '\n')
        if(n > 1)
            break;
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
0 1 2 3 4 5 6 7 8 9 
0:0 2:2 4:4 6:6 8:8 
Hello
3 1 4 1 5 9 
Loop start
Loop test
Iteration 1
Loop test
Iteration 2
Loop test

二次

另见

range-for loop

executes loop over range (since C++11)

c文件

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

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

扫码关注腾讯云开发者

领取腾讯云代金券