All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: About Question enthuware.ocajp.i.v8.2.1059 :
PostPosted: Fri Nov 18, 2016 9:07 am 
Offline

Joined: Thu Nov 10, 2016 3:07 am
Posts: 10
Can you explain this?

Code:
public static void main(String[] args){
for(;;)
System.out.println("Hi");
}

The above code compiles and result in an infinite loop when run.

Code:
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?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v8.2.1059 :
PostPosted: Fri Nov 18, 2016 10:25 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
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.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v8.2.1059 :
PostPosted: Thu Jan 26, 2017 8:27 am 
Offline

Joined: Thu Jan 26, 2017 8:23 am
Posts: 1
i can't get it

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

How excalty i can use .05 value?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v8.2.1059 :
PostPosted: Thu Jan 26, 2017 11:05 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
Math.random returns a double, so the expression is only really comparing two doubles.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v8.2.1059 :
PostPosted: Mon Mar 20, 2017 7:03 pm 
Offline

Joined: Sat Jan 14, 2017 5:01 pm
Posts: 9
I am a little bit confused on this part:
Code:
 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?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v8.2.1059 :
PostPosted: Mon Mar 20, 2017 10:45 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
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.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum