Author |
Topic: Strange nested loop behaviour (Read 832 times) |
|
tsh73
Full Member
member is offline


Gender: 
Posts: 210
|
 |
Re: Strange nested loop behaviour
« Reply #7 on: Oct 9th, 2015, 5:05pm » |
|
My guess that if last loop instruction executed was "for x=..." and somehow interpreter hit line "next y", it has information that something went wrong.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Strange nested loop behaviour
« Reply #8 on: Oct 9th, 2015, 6:38pm » |
|
on Oct 9th, 2015, 5:05pm, tsh73 wrote:| My guess that if last loop instruction executed was "for x=..." and somehow interpreter hit line "next y", it has information that something went wrong. |
|
That isn't necessarily considered 'wrong'. The following program reports no error in QBASIC, BBC BASIC, Liberty BASIC, Just BASIC or LBB:
QBASIC Code: FOR y = 1 TO 4
FOR x = 1 TO 4
PRINT x, y
GOTO skip
NEXT x
skip:
NEXT y BBC BASIC Code: FOR y = 1 TO 4
FOR x = 1 TO 4
PRINT x, y
GOTO (skip)
NEXT x
(skip)
NEXT y LB/JB/LBB Code: FOR y = 1 TO 4
FOR x = 1 TO 4
PRINT x, y
GOTO [skip]
NEXT x
[skip]
NEXT y However, interestingly, the results differ. In QBASIC, BBC BASIC and LBB the following output is generated:
Code: But in LB 4.04, LB 4.5.0 and Just BASIC the following output is generated:
Code: Note that in the former case x remains constant and y changes, but in the latter case y remains constant and x changes. LB/JB is doing something very strange, but I do not understand what.
Richard.
|
|
Logged
|
|
|
|
wscbill
New Member
member is offline


Posts: 2
|
 |
Re: Strange nested loop behaviour
« Reply #9 on: May 17th, 2017, 3:01pm » |
|
Similar results occur in LB using nested DO loops. In my situation the branch out of the inner loop was the result of a menu control handler using a branch address, rather than an explicit "goto" statement. Took a bit of testing to figure out what was going on.
What appears to be happening is that when a FOR or DO loop is encountered, the address of top of the FOR/DO loop is pushed onto a stack. The NEXT/LOOP statements use the address at the top of the stack to loop back.
At the completion of the loop, when NEXT/LOOP statement falls through, the top address is popped off the stack. When loops are nested, the pushes and pops keep the loop addresses synchronized with their respective loop code.
Branching out of the loop and bypassing the NEXT statement, also bypasses the pop. So the loop address is wrong for the code being executed.
In the example code, the branch to [skip] bypasses the "next x", so the loop address of the inner loop (for x=1) is still on the top of the stack. When "next y" is executed, it loops back to "for x=1" , instead of to "for y=1".
Code:
for y = 1 to 3
for x = 1 to 5
if x = 4 then goto [skip]
print x, y
next x
[skip]
next y
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Strange nested loop behaviour
« Reply #10 on: May 17th, 2017, 4:47pm » |
|
Both LB and LBB forbid jumping out of a loop using GOTO, and 'anything' might happen if you break this rule. The EXIT statement is specifically provided for this case, so there is no reason ever to risk using GOTO:
Code:
for y = 1 to 3
for x = 1 to 5
if x = 4 then exit for
print x, y
next x
next y In modern structured programming GOTO is obsolete, and I would suggest it should ideally not be used in any circumstances whatever. However some people feel this is over-zealous.
Richard.
|
|
|
|
|