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?