Which two statements are true?

Given the for loop construct:
<code>
for ( expr1 ; expr2 ; expr3 ) {
statement;
}
</code>
Which two statements are true?

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.

Explanation:

The for statement have this forms:

for (init-stmt; condition; next-stmt) {
body
}

There are three clauses in the for statement.
The init-stmt statement is done before the loop is started, usually to initialize an iteration variable.
The condition expression is tested before each time the loop is done. The loop isn’t executed if the
boolean expression is false (the same as the while loop).
The next-stmt statement is done after the body is executed. It typically increments an iteration
variable.
Incorrect answers:
A: A try for a counter-example:
for (int 1 : a) {
}
This is a correct statement. However, Expr2 and Expr3 are here empty expressions, so it is still in the
format defined above.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Alex

Alex

The C to be correct it should say :
When expr2 evaluates to false,the loop terminates. It is evaluated only after each iteration through the loop except the first iteration.

Nalin

Nalin

B.The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop begin.
And
C.When expr2 evaluates to false, the loop terminates. It is evaluated only after each iteration
through the loop.