File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for ( ; ; )

 
Brian Albin
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?"
 
Brian Albin
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So ...

should print "true"
(I don't have a Java SDK on this machine to try it now.)
Brian
 
Tom Blough
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the reason some programmers prefer . It's easier to read at first glance.
Tom Blough
 
Brian Albin
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic