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 可以省略沒有關係。
