This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for ( ; ;)" Watch "for ( ; ;)" New topic
Author

for ( ; ;)

Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Posts: 138
When we write the for-loop header as follows, what does it mean?

for(; ;)
{

}


Thanks

[edited to disable smilies]
[ May 22, 2008: Message edited by: Jeanne Boyarsky ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Abder,
That loop runs forever since there is no end loop condition.

Normally, it looks like this:

The first piece initializes, the second piece checks if we are done and the third piece moves us closer to the end.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Posts: 138
Thank you very much.
[ May 22, 2008: Message edited by: Abder-Rahman Ali ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

A bit more elaborate:

A for loop header has three parts: the initializer, the condition and the increment. The loop will first execute the initializer part, and then while the condition returns true it will execute the loop body, then the increment.

In this example there is no initializer, no increment and no condition. If omitted, the condition yields true, so the loop is a synonym for the following:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Originally posted by Rob Prime:
A bit more elaborate:

A for loop header has three parts: the initializer, the condition and the increment. ...


Not Always


Jeanne Boyarsky :
The first piece initializes, the second piece checks if we are done and the third piece moves us closer to the end.

[ May 23, 2008: Message edited by: Vishal Pandya ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

But it is still mostly called the increment statement. Wikipedia calls it the "counting expression".
You're right that it doesn't have to be an increment on integers, or even needs to be present.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Rob Prime:
But it is still mostly called the increment statement. Wikipedia calls it the "counting expression".
You're right that it doesn't have to be an increment on integers, or even needs to be present.


The Java Language Spec calls it the "ForUpdate" part, which isn't even a word (14.14.1)! I agree -- I think most people call it the "increment."


[Jess in Action][AskingGoodQuestions]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

I agree too. I've heard it called the increment. I choose my words more carefully so as not to imply it couldn't go backwards.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

I still remember it from when I was programming in C -- the three components of the "for" loop, in C, were the initializer, condition, and reinitializer.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Both Guy Steele and K&R call them "expression1", "expression2" and "expression3" !
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Well that certainly is self-explanatory.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for ( ; ;)
 
Similar Threads
Insert the current date
NullPointerException in PreparedStatement...
I am Looking for a SCJP 6 voucher
AJAX on IE
SCJP6 book page 298 errata