分享方式:


for 陳述式 (C++)

重複執行陳述式,直到條件變成 false。 如需範圍型 for 陳述式的詳細資訊,請參閱 範圍型 for 陳述式 (C++)。 如需 C++/CLI for each 陳述式的相關資訊,請參閱 for eachin

語法

for ( init-expression ; cond-expression ; loop-expression )
statement

備註

使用 for 陳述式建構必須執行指定次數的迴圈。

如下表所示,for 陳述式包含三個選擇性部分。

for 迴圈元素

語法名稱 何時執行 描述
init-expression for 陳述式之前的任何其他元素,只執行 init-expression 一次。 之後就會將控制項傳遞給 cond-expression 通常用來初始迴圈索引。 它可能包含運算式或宣告。
cond-expression 在執行 statement 的每個反覆項目之前,包括第一個反覆項目。 除非 statement 判斷值為 true (非零),否則不會執行 cond-expression 判斷值為整數類型的運算式或具有整數類型明確轉換的類別類型。 通常用來測試迴圈終止準則。
loop-expression 每個 statement 反覆項目的結尾。 在 loop-expression 執行後會評估 cond-expression 通常用來遞增迴圈索引。

下列範例示範使用 for 陳述式的不同方式。

#include <iostream>
using namespace std;

int main() {
    // The counter variable can be declared in the init-expression.
    for (int i = 0; i < 2; i++ ){
       cout << i;
    }
    // Output: 01
    // The counter variable can be declared outside the for loop.
    int i;
    for (i = 0; i < 2; i++){
        cout << i;
    }
    // Output: 01
    // These for loops are the equivalent of a while loop.
    i = 0;
    while (i < 2){
        cout << i++;
    }
    // Output: 01
}

init-expressionloop-expression 可以包含以逗號分隔的多個陳述式。 例如:

#include <iostream>
using namespace std;

int main(){
    int i, j;
    for ( i = 5, j = 10 ; i + j < 20; i++, j++ ) {
        cout << "i + j = " << (i + j) << '\n';
    }
}
/* Output:
    i + j = 15
    i + j = 17
    i + j = 19
*/

loop-expression 可以遞增或遞減,也可以利用其他方式修改。

#include <iostream>
using namespace std;

int main(){
for (int i = 10; i > 0; i--) {
        cout << i << ' ';
    }
    // Output: 10 9 8 7 6 5 4 3 2 1
    for (int i = 10; i < 20; i = i+2) {
        cout << i << ' ';
    }
}
// Output: 10 12 14 16 18

for 迴圈會在執行statement內的break傳回goto (for迴圈外標記的陳述式) 時終止。 for 迴圈中的 continue 陳述式只會終止目前的反覆項目。

如果省略cond-expression,則會將其視為true,而且for迴圈不會在statement內沒有breakreturngoto時終止。

雖然 for 陳述式的三個欄位通常用於初始化、測試終止及遞增,但它們不只限於這些用途。 例如,下列程式碼會列印數字 0 到 4。 在這個範例中,statement 是 Null 陳述式:

#include <iostream>
using namespace std;

int main()
{
    int i;
    for( i = 0; i < 5; cout << i << '\n', i++){
        ;
    }
}

for 迴圈和 C++ 標準

C++ 標準說明在 for 迴圈中宣告的變數應在 for 迴圈結束時超出範圍。 例如:

for (int i = 0 ; i < 5 ; i++) {
   // do something
}
// i is now out of scope under /Za or /Zc:forScope

根據預設,在 /Ze 下,在 for 迴圈中宣告的變數會保持在範圍中,直到 for 迴圈的封閉範圍結束。

/Zc:forScope 允許在 for 迴圈中宣告之變數的標準行為,而不需要指定 /Za

您也可以使用 for 迴圈的範圍差異在 /Ze 底下重新宣告變數,如下所示:

// for_statement5.cpp
int main(){
   int i = 0;   // hidden by var with same name declared in for loop
   for ( int i = 0 ; i < 3; i++ ) {}

   for ( int i = 0 ; i < 3; i++ ) {}
}

此行爲更精確地模擬在 for 迴圈中所宣告之變數的標準行為,這會要求在 for 迴圈中宣告的變數於迴圈結束後超出範圍。 在 for 迴圈中宣告變數時,編譯器會在內部將它升級至 for 迴圈封閉範圍中的區域變數。 即使已經有相同名稱的區域變數,也會將它升級。

另請參閱

反覆運算陳述式
關鍵字
while 陳述式 (C++)
do-while 陳述式 (C++)
範圍型 for 陳述式 (C++)