aspose file tools*
The moose likes Beginning Java and the fly likes for ( ; ; ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for ( ; ; )" Watch "for ( ; ; )" New topic
Author

for ( ; ; )

Brian Albin
Greenhorn

Joined: Mar 02, 2004
Posts: 24
I know that for ( ; ; ) produces an infinite loop.
My question is ... why?
The termination section is blank, so it would seem that the loop should immediately fall out rather than loop forever.
Brian
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
In java an empty expression is a valid one, and a valid expression is a TRUE expression. And since the expression in the for loop is evaluated to true, then it will loop forever.
Think of it this way, "you can�t falsify an expression that is not there, right?"


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Brian Albin
Greenhorn

Joined: Mar 02, 2004
Posts: 24
So ...

should print "true"
(I don't have a Java SDK on this machine to try it now.)
Brian
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
This is the reason some programmers prefer . It's easier to read at first glance.
Tom Blough


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Brian Albin
Greenhorn

Joined: Mar 02, 2004
Posts: 24
I went to a machine with a compiler and tried the following:

This will not compile. The expression used in an if statement or a while statement cannot be blank.
Brian
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by Brian Albin:
So ...

should print "true"
(I don't have a Java SDK on this machine to try it now.)
Brian

This code will not even compile. My above explanantion was not clear, what i i meant by an expression, a valid "compiler line" that ends with a ';' .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Vicken's interesting assertion about "empty expressions" notwithstanding, the correct answer is simply "because." See the appropriate section of the Java Language Specificiation where the "for" loop is defined. You'll see that the "Expression" part is optional, and that by definition, if it's not present, the loop proceeds as if it were present and evaluated to "true".
There is no blanket statement anywhere in the JLS about "empty expressions", and so your example code which tries to omit the expression in a "if" is, as you'd expect, invalid code.


[Jess in Action][AskingGoodQuestions]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

What i was trying to say is that the following code is totally legal in java, as you can see there is no expression proceeding the ';', and yet the compiler won't complain about it.

Yes, you're correct, empty statements are legal. But note that an expression and a statement are not the same thing -- for example,

does not compile because "x < 10" is an expression, not a statement, and bare expressions aren't valid in a method body -- even though "x < 10" can be, for example, the expression part of a "for" loop.
I know this is a really picky distinction, but a lot of people are studying for various certification tests, and those tests can be very picky.
[ March 16, 2004: Message edited by: Ernest Friedman-Hill ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
I didn't post that
EFH-
You are absolutely right, I used wrong terminologies here, I could've said statement instead of saying: an expression that ends with ';'
And for those who are wondering, I am not replying to myself.
Thanks for the editing EFH, you made me look smarter.
[ March 16, 2004: Message edited by: Vicken Karaoghlanian ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for ( ; ; )