Closed dgarroDC closed 5 years ago
I think the STEP
could be optional, and increment the variable by 1
or -1
depending on x < y
or not, what do you think?
maybe from X to Y step Z store in <variable> do
would be more readable?
but then we lose the for
part, which is a problem.
the step part shouldn't be optional imo, as that goes against the "rigidness" of the language,
anyways, I think that this is an awesome addition!
This adds a FOR statement, following this syntax [...] where i is a number variable and x, y and z are number variables or literals.
I love it! Thank you so very much for this wonderful contribution!
At the start of an iteration, the condition i <= y is evaluated if x < y and i >= y otherwise.
This is what I like the most! Wonderful!
I used NEXT to mark the end of a FOR loop, but it could be REPEAT like in the WHILE loop without any trouble, if you prefer it.
I like NEXT
, I would leave it like that. Maybe, just maybe, I'd allow
REPEAT
as well, but next is alright with me. I mean, when someone says
FOR
you immediately think of FOR-NEXT
, so I think that's half the battle
won right there.
I think the STEP could be optional, and increment the variable by 1 or -1 depending on x < y or not, what do you think?
I agree with @fireasembler, it shouldn't be optional, it gives more clarity to the matter. Just what they said.
Then firesaembler said:
maybe from X to Y step Z store in
do would be more readable? but then we lose the for part, which is a problem.
I do think losing the for
part is a problem. When you need a for loop
in a language you search 'how to write a for
loop in X language',
renaming it would make it harder to find (and, for people with any programming
background) to understand.
I love this, thanks a lot to @dgarroDC and thanks to you fireasembler for your valuable input! I'm merging this right now!
Please @dgarroDC document this new feature in the docs and add for
loop tests to the ldpl test battery. If you can't do it, please tell me and I'll try to do it myself. Thank you!
@Lartu I suggested changing it to from X to Y step Z store in <var> do
because imo the for loop syntax is best used as the syntax of an iteration loop (think python for X in Y
)
@fireasembler I understand, and I do agree with you. I like the from
syntax more, but it could be confusing for people not used to the language. In python they still keep the for
keyword, it's somewhat universal, I wouldn't dare replacing it, even though your syntax is clearer.
While I was writing the tests, I found a bug when x
was equal to y
, it looped forever when the step was positive instead of doing just one iteration. To fix it, the condition is based on the step, (i <= y
if z >= 0
). This actually makes more sense (and it is what VB does), because yo mark the intended direction of the for with the step, not the range. In fact, it's useful when you iterate lists, because if it's empty you have a FOR
from 0
to -1
with step 1
and you don't want to iterate anything in this case, instead of looping forever.
Haha, that's true. Wonderful, thank you very much for this and for uploading tests to the test battery!
This adds a FOR statement, following this syntax:
where
i
is a number variable andx
,y
andz
are number variables or literals.First, it assigns
x
toi
and starts an iteration. At the start of an iteration, the conditioni <= y
is evaluated ifx < y
andi >= y
otherwise. If the condition passes, theFOR
body is executed, if it fails the loop ends. After the body is executed,i
is incremented byz
and a new iteration is started (checking the condition and so on).The
BREAK
andCONTINUE
statements can be using insideFOR
loops.I used
NEXT
to mark the end of aFOR
loop, but it could beREPEAT
like in theWHILE
loop without any trouble, if you prefer it.Examples:
outputs
0 2 4 6
.outputs
5 4 2 1 0
.