LB Booster
« Strange nested loop behaviour »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:53am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Strange nested loop behaviour  (Read 832 times)
tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx 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.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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:
1             1
1             2
1             3
1             4 

But in LB 4.04, LB 4.5.0 and Just BASIC the following output is generated:

Code:
1             1
2             1
3             1
4             1 

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.
User IP Logged

wscbill
New Member
Image


member is offline

Avatar




PM


Posts: 2
xx 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 
 


User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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.
« Last Edit: May 17th, 2017, 4:48pm by Richard Russell » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls