Re: for loop (c-lang)



On Wed, 28 Feb 2007 09:48:21 +0100, "Maarten Wiltink"
<maarten@xxxxxxxxxxxxxxxxxx> waffled on about something:

"Dodgy" <Dodgy@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:h2h8u2hq4sp5aj6k1qtna11vij4p4gmbsn@xxxxxxxxxx
On 26 Feb 2007 05:18:51 -0800, mohdalibaig@xxxxxxxxx waffled on about
something:

why does this loop execute once
for( ; 0 ;)
printf("Why"); ???

For loops will always execute at least once as the end condition is
only ever checked at the end of the code block, where the "next" would
be in basic.

Bollocks. A for loop is a while loop.

Well it might be in Delphi, not sure, can honestly say if I've ever
tried to do a loop zero times in a for, but from the programming
background I come from "for" and "repeat" loops have the comparison at
the end, a "while" has it at the beginning.

I guess I'll have to blow the dust of my asm programming and see what
Delphi pumps out.

Same goes for repeat.. until... The conditional check is at the end
on the until.

Compilers may lay out the generated code like that. But in the case of
for loops or while loops, you'll find a jump at the beginning to start
with the condition test anyway.

I can't remember the name of the diagrams they taught me at college
many years ago, but you'd draw a loop as quite literally a loop, and
put a pair of parallel lines across the line where the loop
conditional would occur. "For" and "Repeat" had them at the end.
"While" at the beginning.

However a while has the condition at the beginning, so it's possible
for a while loop to never execute.

As for why that code with no loop variable actually compiles, well
that's cos C is designed to make even the simplest piece of code you
wrote last week look like the output of a 5 rotor enigma machine.

The entire concept of a loop variable is absent. This is not a for-next
loop. Using the comma operator, you can keep any number of loop variables
in your for loop, but they're yours, not the compiler's.

I do agree with the Enigma observation.

Glad about that bit at least!

Dodgy.
--
MUSHROOMS ARE THE OPIATE OF THE MOOSES
.



Relevant Pages