> My personal preference is the while loop.
> > My personal preference is the while loop.
> I much prefer the for loop, except when there is no loop variable.
My opinion on the for statement:
It is useful when there are 2 simple statements (initializer and
post-loop), one conditional expression, and 1 more statement (compound
or otherwise)
The iterator concept does NOT fall into that category, unless you write
it like this:
E e;
for (Iterator<E> i = getIterator(); i.hasNext(); doSomething(e)) {
e = i.next();
}
Which IMHO is ugly.
Although, like you said
> I notice that the sun rises and sets irrespective of my preference, though.
Happens to be true of me too.
bjeremy - 22 Dec 2006 02:24 GMT
> The iterator concept does NOT fall into that category, unless you write
> it like this:
[quoted text clipped - 4 lines]
>
> Which IMHO is ugly.
How's this then?
for (Iterator<E> i = getIterator(); i.hasNext(); ){
doSomething(i.next());
}
Lew - 22 Dec 2006 03:39 GMT
> My opinion on the for statement:
> It is useful when there are 2 simple statements (initializer and
> post-loop), one conditional expression, and 1 more statement (compound
> or otherwise)
That use case lies far over toward the "for" end of the seesaw.
> The iterator concept does NOT fall into that category, unless you write
> it like this:
[quoted text clipped - 4 lines]
>
> Which IMHO is ugly.
That use case lives nearer the middle.
I happen to like that idiom, except that I declare "E e" inside the loop and
put the doSomething() there, too, but I understand perfectly well why it seems
ugly.
I rarely will put a workhorse expression inside the for () setup; they belong
in the body.
My version would be (assuming a collection as the progenitor of the iterator):
for ( Iterator<E> iter = collection.getIterator(); iter.hasNext(); )
{
E entry = iterator.next();
doSomething( entry ); // or just doSomething( iterator.next() )
}
In most cases, that becomes
for( E entry : collection )
{
doSomething( entry );
}
anyway, and the ugliness vanishes very far.
>> I much prefer the for loop, except when there is no loop variable.
I lied. I use for() {}, while () {} and do {} while ();.
I prefer all three. I like "for ( ;; )" to set up unbounded loops.
- Lew
John Ersatznom - 22 Dec 2006 09:25 GMT
> I prefer all three. I like "for ( ;; )" to set up unbounded loops.
Yuck! I have always used (and usually see everyone else use) "while
(true)"...
Lew - 22 Dec 2006 13:22 GMT
>> I prefer all three. I like "for ( ;; )" to set up unbounded loops.
> Yuck! I have always used (and usually see everyone else use) "while
> (true)"...
Yuck? Here we have a perfectly useful idiom purpose-built to handle just this
situation. I wonder why that seems so hideous to you.
It's nice to have choices.
- Lew
Patricia Shanahan - 22 Dec 2006 15:26 GMT
>>> I prefer all three. I like "for ( ;; )" to set up unbounded loops.
>
[quoted text clipped - 7 lines]
>
> - Lew
The difference is that the first time I saw a "for(;;)" I had to think
to work out that it was going to loop forever. It depends on
understanding the effect of omitting the continuation test.
The first time I saw a "while(true)" it was instantly obvious that the
intent was to loop as long as true is true.
Patricia
Lew - 23 Dec 2006 04:29 GMT
> The difference is that the first time I saw a "for(;;)" I had to think
> to work out that it was going to loop forever. It depends on
> understanding the effect of omitting the continuation test.
>
> The first time I saw a "while(true)" it was instantly obvious that the
> intent was to loop as long as true is true.
You seem to imply that a computer language should be explicable to those who
do not know that language.
There is a case for that. OTOH, the for ( ;; ) idiom has been around for
decades and should be familiar to anyone versed in the C/Java family of languages.
OTOOH, I might switch to using while ( true ) myself for the very reason you
state.
OTOOOH, the for ( ;; ) construction is so ugly-cute, and ugly-clever, and
ugly-elitist, that I don't think I will ever give it up entirely.
- Lew
Patricia Shanahan - 23 Dec 2006 05:22 GMT
>> The difference is that the first time I saw a "for(;;)" I had to think
>> to work out that it was going to loop forever. It depends on
[quoted text clipped - 9 lines]
> decades and should be familiar to anyone versed in the C/Java family of
> languages.
"know the language" is not a binary state. I prefer my code to be
readable by as many people as possible.
I have trouble imagining someone understanding "for(;;)" but not
"while(true)". I'm not so sure the other way round.
Patricia