Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Fri Nov 18, 2016 9:07 am
by mj.anjuthan
Can you explain this?

Code: Select all

public static void main(String[] args){
for(;;)
System.out.println("Hi");
}
The above code compiles and result in an infinite loop when run.

Code: Select all

public static void main(String[] args){
for(;;);
System.out.println("Hi");
}
But when a semi-colon is added at the end of 'for loop', then the code fails to compile.

error: unreachable statement
print("hi");
^

How does the semi-colon affects?

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Fri Nov 18, 2016 10:25 pm
by admin
That is just how the compiler parses the code. A semicolon is considered an empty statement but it is still a statement. Since the for in this case does not have a code block i.e. { }, the first statement after the for declaration becomes the part of the for block. Thus, for(;;); is equivalent to :
for(;;){
;
}

Now, since this for loop never ends, any statement after the for block i.e. System.out.println("Hi") becomes unreachable. Without the semicolon, the print statement becomes the part of the for loop.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Thu Jan 26, 2017 8:27 am
by shownofear
i can't get it

for(;;){     if(Math.random()<.05) break; }

How excalty i can use .05 value?

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Thu Jan 26, 2017 11:05 am
by admin
Math.random returns a double, so the expression is only really comparing two doubles.

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Mon Mar 20, 2017 7:03 pm
by dxie1154
I am a little bit confused on this part:

Code: Select all

 for(;;Math.random()){
System.out.println("true");
}
I had thought the third spot of a for loop, or in this case where Math.random() currently is at, should have some sort of increment/decreasing operation going on, like i++.
But in this case, it is just returning a double, not increase or decreasing anything.
How is this valid?

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Mon Mar 20, 2017 10:45 pm
by admin
The third spot is commonly used for incrementing the loop interation variable but this is not a must. Technically, you can have any expression (and even multiple expressions separated by a comma) that is given in the list below.

Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression

For example, the following examples are valid -

for(int i=0; i<10; System.out.println(i),System.out.println("aaa")); //This will keep looping for ever because i is not being incremented at all.

for(int i=0; i<10; i=20, System.out.println(i),System.out.println("aaa")); //This will loop just once.


HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Thu Mar 08, 2018 9:33 am
by Kevin30

Code: Select all

for(;;Math.random()){
    System.out.println("true");
}
is valid, while

Code: Select all

for(;;Math.random()<0.5){
    System.out.println("true");
}
is not valid.

The explanation for this is that in the third part of a for loop only "Assignment, PreIncrementExpression, PreDecrementExpression, PostIncrementExpression, PostDecrementExpression, MethodInvocation, and ClassInstanceExpression" are valid.

Therefore, Math.random<0.5 which is a boolean, is not valid.

However, the result of a MethodInvocation can be a boolean also.

So, why is a boolean (Math.random<0.5) not valid for the third part of a for loop, while a MethodInvocation (which could result in a boolean) is valid?

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Thu Mar 08, 2018 11:37 am
by admin
Because language designers decided to allow method invocation but not an expression :)

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Fri Jan 08, 2021 10:37 am
by Denyo1986
I think the way the question was written is a little ambiguous.
What is a "valid" for loop? One that compiles? Or one that does not loop infinitely?

I read it the way that it will compile, in which case option 4 would be true.

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Fri Jan 08, 2021 11:05 am
by admin
The problem statement has now been updated to:
"Which of the following code fragments compile without any error?"

thank you for your feedback!

Re: About Question enthuware.ocajp.i.v8.2.1059 :

Posted: Fri Jan 08, 2021 12:55 pm
by Denyo1986
I find it great how quickly you guys react and adapt. Good work. Thanks.