Good point, I’ll change 10 to n in the post.
How do you implement a “for” loop from 0 to n with one jump, if n isn’t known and can be 0?
i=0
Label 1
[stuff]
i=i+1
If i ⇐ n
Goto 1
I meant a “for” loop with i < n...
What’s wrong with the implementation Dacyn gave?
It seems to me, that Dacyn’s code executes [stuff] at least once for any n. But iff n <= 0, original while loop does not execute its body. Dacyn’s code looks like a do-while loop.
n
n <= 0
while
do-while
Oh right. For loops check the condition before entering the body, just like while loops and unlike do-while loops. Thanks.
Good point, I’ll change 10 to n in the post.
How do you implement a “for” loop from 0 to n with one jump, if n isn’t known and can be 0?
i=0
Label 1
[stuff]
i=i+1
If i ⇐ n
Goto 1
I meant a “for” loop with i < n...
What’s wrong with the implementation Dacyn gave?
It seems to me, that Dacyn’s code executes
[stuff]
at least once for anyn
. But iffn <= 0
, originalwhile
loop does not execute its body. Dacyn’s code looks like ado-while
loop.Oh right. For loops check the condition before entering the body, just like while loops and unlike do-while loops. Thanks.