In a typical FOR statement it is unambigoues:
FOR variable=3  TO=6  STEP=+1

the FOR-loop body expands four times, giving the variable values 3,4,5,6.

What if the range has different sign than step value?
FOR variable=6  TO=3  STEP=+1

Imagine this is a preprocessing assembly language.
Would you prefer the FOR-loop body be
0) not expanded at all
1) expanded just once with variable=6
Posted on 2004-04-22 16:07:36 by vit$oft
with such a for statement, I would expect the counter to wrap around and continue until it hits 3 - basically doing "a lot" of iterations.

I prefer C-style for loops anyway :)
Posted on 2004-04-22 16:23:15 by f0dder
Not expanded.

The most common case would be

FOR i=1 to=N step=+1

where N is supplied by an expression that can sometimes evaluate to 0.
Posted on 2004-04-22 16:27:51 by tenkey
FOR variable=6 TO=3 STEP=+1

...this statement is an error, imho. We are talking HLL and it is impossible to get to 3 from 6 through increment. Please, provide proper error messages, or much more complex implementations will produce hard to find errors.
Posted on 2004-04-22 17:30:09 by bitRAKE
What he's asking is, should the loop run once, with the value 6, then increment (Now variable = 7) then check to see if it's 'greater than' 3, of course it isn't, but the loop ran once anyway and only checked the value at the end of the loop. I would say, the loop should never run, that means, it should check to see if it's greater than 3 (because our step is +1) first, if it isn't, then don't run the loop. This type of loop won't generate errors at all, it just won't execute or be compiled. The C version would be, for (int i = 6; i < 3; i++), the C version won't generate errors either, but a good compiler should show warnings.
Posted on 2004-04-23 00:51:54 by SubEvil
afaik, the equal c-code for(int i=6;i<3;i++) doesn't even run once, that whole loop will be skipped as C usually checks the condition before entering the loop.

greets, hartyl
Posted on 2004-04-23 04:17:15 by hartyl
Should check first, and exit immediately since 6>3. For HLL.
Posted on 2004-04-23 05:46:11 by valy
The loop should only execute if the control expression evaluates to true.
I also think an error should be thrown, or at the very least, a warning.
Posted on 2004-04-23 07:31:07 by Maelstrom
I think it should execute once, there is a fundamental difference between a FOR loop and a WHILE loop. In the case of a FOR loop you are garanteed 1 execution in a WHILE loop it can be 0. Since you specified a FOR loop, it should execute once, it is the programmers resposibility to choose the right type of loop for the range of parameters.

[b]FOR[/b]

L1:
cmp eax,100
jge >L2
;code
jmp <L1
L2:

[b]WHILE[/b]
jmp >L2
L1:
;code
L2:
cmp eax,100
jl <L1
Posted on 2004-04-23 09:09:11 by donkey
Donkey, that is incorrect. Both the "FOR loop" and the "WHILE loop" you presented are the equivalent from loop iteration point-of-view. In my opinion, a FOR loop is like a WHILE loop, except that there may be a guaranteed side-effect for each iteration. So, answering the original question, it should emit a warning if it can, and perform zero iterations.
Posted on 2004-04-23 11:52:45 by death
Hi Death,

LOL you're right, I typed them on the fly and they are both variants of a WHILE loop, thanks for pointing that out..

[b]FOR[/b]

L1:
;code
cmp eax,100
jl <L1
Posted on 2004-04-23 12:10:38 by donkey
FOR eax=start_value TO end_value STEP step
[b]FOR[/b]:

mov eax, start_value

; loop code

add eax, step
cmp eax, end_value
jle [b]FOR[/b]
This is the generic code for a FOR loop.
Posted on 2004-04-23 12:23:05 by bitRAKE
tenkey , you're right but STEP, if omitted, will default to either +1 or -1, depending on the range direction.


FOR i=3 TO=5 STEP=+1 executes 3 times (variant 0) or 3 times (variant 1)
FOR i=3 TO=4 STEP=+1 executes 2 times (variant 0) or 2 times (variant 1)
FOR i=3 TO=3 STEP=+1 executes 1 times (variant 0) or 1 times (variant 1)
FOR i=3 TO=2 STEP=+1 executes 0 times (variant 0) or 1 times (variant 1)
FOR i=3 TO=1 STEP=+1 executes 0 times (variant 0) or 1 times (variant 1)

FOR i=3 TO=5 STEP=-1 executes 0 times (variant 0) or 1 times (variant 1)
FOR i=3 TO=4 STEP=-1 executes 0 times (variant 0) or 1 times (variant 1)
FOR i=3 TO=3 STEP=-1 executes 1 times (variant 0) or 1 times (variant 1)
FOR i=3 TO=2 STEP=-1 executes 2 times (variant 0) or 2 times (variant 1)
FOR i=3 TO=1 STEP=-1 executes 3 times (variant 0) or 3 times (variant 1)

FOR i=3 TO=5 executes 3 times
FOR i=3 TO=4 executes 2 times
FOR i=3 TO=3 executes 1 times
FOR i=3 TO=2 executes 2 times
FOR i=3 TO=1 executes 3 times


SubEvil and other C guys, you got me right, but I am not sure
that everything must go the C way. In fact, I do not use C because
it does many things the other way round than I would choose if I were its creator.
death, what if the FOR loop is more equivalent to REPEAT/UNTIL, which always expands at least once?

So far only donkey and bitRAKE would choose variant 1),
f0dder is only joking (I hope ;=) and all others prefer 0).
I already have implemented the donkey's way but perhaps I will change it according to the poll.
Thank you all for participating.
Posted on 2004-04-23 13:59:59 by vit$oft
Well I'd support variant 0. It seems more intutive.
Posted on 2004-04-24 09:38:45 by Eóin

FOR variable=6 TO=3 STEP=+1

...this statement is an error, imho. We are talking HLL and it is impossible to get to 3 from 6 through increment. Please, provide proper error messages, or much more complex implementations will produce hard to find errors.


What if the value of the loop variable is in some variable which is calculated at run time.

so its something like -


[I]//using C style syntax for declaring the variable[/I]

int a,b;
cin>>a; //the input is 6
cin>>b; // the input is 3

For variable=a TO=b STEP=+1


Now how will the code generated execute, cause the compiler cant give any warnings now, cause the inputs are done at runtime. :confused:


edit: also if the loop executes why should it execute only once. The loop invariant says that the variable should be equal to 3 not less than or equal to 3 . so why should it execute only once???
Posted on 2004-04-24 13:00:57 by clippy

death, what if the FOR loop is more equivalent to REPEAT/UNTIL, which always expands at least once?

If it's a REPEAT/UNTIL (I'd rather call it a DO-WHILE) then, obviously, it should execute once. But like I said, I don't see a FOR loop as a DO-WHILE one - I follow the C definition.
Posted on 2004-04-24 13:57:05 by death
*cough, cough* :)
Any answers to my question?
Posted on 2004-04-25 08:43:02 by clippy
clippy, quite simply: the program should stop and produce an error.
Posted on 2004-04-25 12:12:20 by bitRAKE

Would you prefer the FOR-loop body be
0) not expanded at all
1) expanded just once with variable=6


I guess it would depend on whether you grew up on:

0) Pascal/C/etc.
1) FORTRAN

:-)

Cheers,
Randy Hyde
Posted on 2004-05-04 15:24:50 by rhyde