Which two statements are true?

Given the for loop construct:
for ( expr1 ; expr2 ; expr3 ) {

statement;
}
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.



Leave a Reply 8

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


mnish

mnish

The first part of C is right but the second not right because the condistion expression is evaluated BEFORE each iteration.

szattila

szattila

You are right.
The correct answer: A,B
A: Enhanced for loop: for(type variable: array){…} , where the array type is the same with the variable type
B: all three expressions are optional, the rest is true too
C: not after but before
D: all three expressions are optional. e.g. infinite loop: for(;;){}
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Jazz

Jazz

B,C
The for statement have this forms:for (init-stmt;conditionbody
}
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.

Alonso

Alonso

You are missing in your answer.
Taken from java tutorial(https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html):

A) The for statement also has another form designed for iteration through Collections and arrays This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read.

B)The initialization expression initializes the loop; it’s executed once, as the loop begins.

Option (C) is partially correct but it’s evaluated BEFORE loop’s inside statements are executed.

Ryan

Ryan

You are stating why C is wrong in your reply! “The init-stmt statement is done before the loop is started”

Vladimir_K

Vladimir_K

A only. Choice 2 already contains 2 statements!

Vladimir_K

Vladimir_K

A only. Choice A already contains 2 statements!

Vladimir_K

Vladimir_K

I was wrong. A and B