Given the for loop construct:



for ( expr1 ; expr2 ; expr3 ) {
    statement;
}

Which two statements are true?

A. This is not the only valid for loop construct; there exits another form of for loop constructor.
B. The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop begin.
C. When expr2 evaluates to false, the loop terminates. It is evaluated only after each iteration through the loop.
D. The expression expr3 must be present. It is evaluated after each iteration through the loop.

題解

選項A,敘述正確,還有foreach的結構,如下:

for ( expr1 : expr2) {
    statement;
}

選項B,敘述正確,expr1是用來初始化for迴圈會用到的變數。

選項C,每次要進入迴圈之前就會判斷expr2,而不是每次迴圈執行之後。每次迴圈執行之後才判斷expr2,比較像是do-while的結構。

選項D,expr3可以省略沒有關係。