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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
mj.anjuthan
Posts: 10
Joined: Thu Nov 10, 2016 3:07 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

shownofear
Posts: 1
Joined: Thu Jan 26, 2017 8:23 am
Contact:

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

Post by shownofear »

i can't get it

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

How excalty i can use .05 value?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Math.random returns a double, so the expression is only really comparing two doubles.

dxie1154
Posts: 9
Joined: Sat Jan 14, 2017 5:01 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Because language designers decided to allow method invocation but not an expression :)

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests