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. <br />- What truth? <br />- That there is no spoon!!!
Joined: Mar 02, 2004
should print "true" (I don't have a Java SDK on this machine to try it now.) Brian
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.
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 ]
Joined: Jul 21, 2003
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 ]