Post by tsh73 on Nov 14, 2022 6:19:34 GMT
It just bite me so I thought I could warn you.
If you have variables used in a FOR as end value, step
like
bevare that changing them in a loop does not change loop execution.
Look:
result:
expected (by me) result:
Now, I can fix that with EXIT FOR:
But for similar change in step variable I do not see a fix (luckily I do not need it)
result
(I expect to have line with i = 8 as well)
EDIT
I did some jumping through the hoops to run qbasic.exe in Win10 64 bit (: DOSBOX)
and I could confirm QBasic befaves exactly like this.
Just one of cases there you think to know how things work, but indeed you aren't.
If you have variables used in a FOR as end value, step
like
for i =1 to k step h
bevare that changing them in a loop does not change loop execution.
Look:
k=5
print "k before loop", k
for i =1 to k
if i = 2 then k = 3
print i,"k inside loop", k
next
print "k after loop", k
result:
k before loop 5
1 k inside loop 5
2 k inside loop 3
3 k inside loop 3
4 k inside loop 3
5 k inside loop 3
k after loop 3
expected (by me) result:
k before loop 5
1 k inside loop 5
2 k inside loop 3
3 k inside loop 3
k after loop 3
Now, I can fix that with EXIT FOR:
k=5
print "k before loop", k
for i =1 to k
if i>k then exit for
if i = 2 then k = 3
print i,"k inside loop", k
next
print "k after loop", k
But for similar change in step variable I do not see a fix (luckily I do not need it)
k=10
h=2
print "k before loop", k
for i =1 to k step h
if i = 7 then h = 1
print i,"h inside loop", h
next
print "k after loop", k
result
h before loop 2
1 h inside loop 2
3 h inside loop 2
5 h inside loop 2
7 h inside loop 1
9 h inside loop 1
h after loop 1
(I expect to have line with i = 8 as well)
EDIT
I did some jumping through the hoops to run qbasic.exe in Win10 64 bit (: DOSBOX)
and I could confirm QBasic befaves exactly like this.
Just one of cases there you think to know how things work, but indeed you aren't.