Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for ( ; ;)

 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much.
[ May 22, 2008: Message edited by: Abder-Rahman Ali ]
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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:
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both Guy Steele and K&R call them "expression1", "expression2" and "expression3" !
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that certainly is self-explanatory.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic