FOR difference with JB/LB4

Discussions relevant to the differences between LBB and LB4 which could affect code compatibility
tsh73
Posts: 44
Joined: Fri Apr 06, 2018 7:58 pm

FOR difference with JB/LB4

Post by tsh73 »

This code
n=10
for i = 1 to n
   print i
   if i = 5 then n = 0
next
types 10 numbers at LB/JB, but only 5 at LBB.

Actually if one open right panel in LBB, FOR loop will look like
 3 FOR i = 1 TO n : WHILE 1 > n EXIT FOR : ENDWHILE
Probably this WHILE kicks in.
guest
Site Admin
Posts: 193
Joined: Tue Apr 03, 2018 1:34 pm

Re: FOR difference with JB/LB4

Post by guest »

tsh73 wrote: Mon Dec 18, 2023 8:38 pm This code
n=10
for i = 1 to n
   print i
   if i = 5 then n = 0
next
types 10 numbers at LB/JB, but only 5 at LBB.
Changing either the loop variable or the limit value inside a FOR loop is bad practice. :shock:

BASICs differ in how FOR loops are implemented; for example in some the loop variable is tested for being in the specified range at the end of the loop (in the NEXT statement) and in others it is tested for being in the specified range at the start of the loop (in the FOR statement).

Similarly in some BASICs the limit (TO) value is evaluated only once, when the loop is initialised, and in others it is evaluated each time around the loop. These differences can result in inconsistent behavior if the loop variable or limit value is changed inside the loop.

I have tried to make LBB emulate LB/JB in most respects, but if you do things which are 'unexpected' or 'non standard' I can't guarantee that they will behave in the same way, and this is a case in which they don't.

What is the code trying to achieve? Why is the variable n seemingly being used for two different things: to hold the limit value (iteration count in this case) and also as some kind of flag?
tsh73
Posts: 44
Joined: Fri Apr 06, 2018 7:58 pm

Re: FOR difference with JB/LB4

Post by tsh73 »

Hello
thanks for answering
Changing either the loop variable or the limit value inside a FOR loop is bad practice.
I know. But this program is big - and it's someone else's program - and I guess it just happened, historically.
And if version of BASIC you use allows it, you will never know it's bad.
I have tried to make LBB emulate LB/JB in most respects, but if you do things which are 'unexpected' or 'non standard' I can't guarantee that they will behave in the same way, and this is a case in which they don't.
Sure. I just post the difference because I stubmbled on it (and someone else might as well step on it again).
What is the code trying to achieve? Why is the variable n seemingly being used for two different things: to hold the limit value (iteration count in this case) and also as some kind of flag?
This is example showing effect

Actual part was from the chess program,
where was a function call that prepares possible moves by current figure
and the loop enumerated those moves.
End of loop variable holds number of moves
And this part is recursive - so it gets called again
But somehow, function returned that value via global variable (now I think it hardly supposed to work with recursion)
Thus, global value got changed - loop parameters got changed
But since JB seemingly caches loop parameters on a first run, and program did not used this variable for anything else, it did not showed.