Re: for loop (c-lang)



"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.


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.


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.

Groetjes,
Maarten Wiltink


.



Relevant Pages