• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

for ( ; ;)

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
[ May 22, 2008: Message edited by: Abder-Rahman Ali ]
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."
 
Jeanne Boyarsky
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both Guy Steele and K&R call them "expression1", "expression2" and "expression3" !
 
Rob Spoor
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well that certainly is self-explanatory.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic