• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about loop construct  RSS feed

 
thomas batycki
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to solve problem with this question ?

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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

We don't simply give out answers: what do you think the correct answers are? Check your spelling carefully: maybe exits isn't quite what you meant to say
I am going to go into pedantic mode and say those questions aren't quite correct: C contains two separate statements which might be both true, both false or one of each. Similarly for D.

You will probably find all the answers in the Java™ Tutorials←link: bookmark it.
 
thomas batycki
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did ask Simon Roberts for opinion about this question
Just want to see if anyone else seen this question. There is answers A, B and B, C for some people.
Spelling are correct!
I can see some details like this:

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


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. The expression expr3 must be present. It is evaluated after each iteration through the loop.

D. When expr2 evaluates to false, the loop terminates.
     It is evaluated only after each iteration through the loop.


Statement have this forms:
for ( init-stmt ; condition ; next-stmt ) {body}

There are three clauses in the for statement.

expr1; init-stmt statement is done before the loop is started, usually to initialize an iteration variable.
( " The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop begin " )

expr2; the condition expression is tested before each time the loop is done. The loop isn't executed if the boolean expression is false.
( "  When expr2 evaluates to false, the loop terminates " )

expr3 (next statement after condition…)
the next-stmt statement is done after the body is executed. It typically increments an iteration variable.
( " It is evaluated only after each iteration through the loop." )

It is badly written question...
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
expr1, expr2, and expr3 are all optional.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thomas batycki wrote:. . . Just want to see if anyone else seen this question.
That would require you giving us details about where the question comes from. You ought to quote such source detail anyway.
There is answers A, B and B, C for some people.
In that case one lot have got it wrong. For that sort of question there is only one correct answer.
Spelling are correct!
Yes, the word “exits〝 is correctly spelt, but “exists”, which you probably want isn't
. . .A. This is not the only valid for loop construct;
      there exits another form of for loop constructor.
You appear to think it is true. If so, then there is the for‑each loop or enhanced for loop. So correct
B. The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop begin.
Again, you seem to think that is true. As CB has said, all three expressions are optional, and the initialisation runs only once if at all, so that sounds correct Ambiguous question; if the first part is missing it never runs at all.
C. The expression expr3 must be present. It is evaluated after each iteration through the loop.
Whiever thought that is true is mistaken. As CB says, that part of the loop may be omitted. So half of that statement is false, so you must regard the whole question as false. As you said, it is a badly written question. If you thought false:
D. When expr2 evaluates to false, the loop terminates.
     It is evaluated only after each iteration through the loop.
That is badly written; under certain circumstances expr2 evaluates to false before the loop ever starts and the loop cannot terminate because it never starts. It can also be evaluated before the loop ever runs, so the other half is false. You seem to think that was false too, so
Statement have this forms:
for ( init-stmt ; condition ; next-stmt ) {body} . . .
Despite the w‍ord expression appearing in the Java™ Tutorials link I have you yesterday, only the middle expression (condition) is actually an expression. The first might be a statement or declaration and initialisation. The increment is usually a statement. The three parts shou‍ld be called initialisation, continuation and increment.

Just to confuse you: You can call a statement like i++; an expression statement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!