Loopy bugs

Anything QL Software or Programming Related.
User avatar
pjw
QL Wafer Drive
Posts: 1286
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Loopy bugs

Post by pjw »

Rather than dumping on poor Dr Jim, perhaps this sort of thing should have a topic of its own.
Those of you on ql-user's will, unfortunately, have seen this one quite recently:

I dont know if this has been documented anywhere, but Im putting it out here as it caused me some grief.

It appears that Q-Liberator zeroes the loop variable on entry to a loop. The following demo, which is acceptable (although perhaps not very elegant) S*BASIC, will not work in the same way in SuperBASIC or when Qlib compiled.

Code: Select all

100 loop = 3
110 cnt = 0
120 PRINT 'Start:'! loop, cnt
130 REPeat loop
140  PRINT loop, cnt
150  cnt = cnt + 1
160  IF cnt >= loop: EXIT loop
170 END REPeat loop
180 PRINT 'End:'! loop, cnt
In the Qlib-compiled version the loop is exited after a single run as the condition cnt >= loop is met immediately, to wit 1 >= 0

The worrying part of this is that while I was figuring out what was wrong, running an embedded routine like this many times, the system crashed due to memory corruption. Whether this was due to the SBASIC or Qlib compiled version (or neither) I cant say. So just beware and keep on progging!

NB: This works across the board, though, as long as you expect loop to be 0

Code: Select all

110 cnt = 3
120 PRINT 'Start:'! loop, cnt
130 REPeat loop
140  PRINT loop, cnt
150  loop = loop + 1
160  IF loop >= cnt: EXIT loop
170 END REPeat loop
180 PRINT 'End:'! loop, cnt


Per
dont be happy. worry
- ?
User avatar
bwinkel67
QL Wafer Drive
Posts: 1187
Joined: Thu Oct 03, 2019 2:09 am

Re: Loopy bugs

Post by bwinkel67 »

I'm not a proficient SuperBASIC user but it's an odd example. I look at "loop" in your example as the REPEAT identifier and not a variable that I would assign anything to or use. So to me it's not like "FOR i=1 TO 10" where you then say "NEXT i" since that syntax asks to increment. With "REPEAT i" you do an "EXIT i" which says to leave the loop. Am I right to assume you an also do a "NEXT i" in a REPEAT? If so then that would be similar to "continue" in C.


stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: Loopy bugs

Post by stevepoole »

Hi Bwinkel,

On a QL, NEXT will increment in a FOR loop, but not in a REPeat loop.
The QL uses NEXT in FOR loops to activate 'loop epilogues', but not in REPeat loops :

100 FOR loop=1 TO 5
110 PRINT loop !!
120 NEXT loop: REMark increments loop and falls through to epilogue on loop=5...
130 PRINT 'end'
140 END for loop
150 :
160 loop=1
170 REPeat loop
180 PRINT loop !!
190 NEXT loop: REMark doesn't increment loop nor fall through to epilogue !!
200 PRINT 'end'
210 if loop=5: EXIT loop
220 END REPeat loop

Regards, Steve.


User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Loopy bugs

Post by NormanDunbar »

Maybe I'm just weird (don't all agree at once!) Or maybe because I used other programming languages, but using a repeat loop name which corresponds to another variable name seems a little daft to me? Happy to discuss if you think otherwise though. I might learn something new.

@bwinkle67

Yes, you are correct, NEXT XXX in a REPeat XXX, loop is the same as continue in C. It returns to the first executable statement after the REPeat statement.


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
Martin_Head
Aurora
Posts: 847
Joined: Tue Dec 17, 2013 1:17 pm

Re: Loopy bugs

Post by Martin_Head »

pjw wrote:It appears that Q-Liberator zeroes the loop variable on entry to a loop. The following demo, which is acceptable (although perhaps not very elegant) S*BASIC, will not work in the same way in SuperBASIC or when Qlib compiled.
Yes it does. If you decompile it, you will find a variable assignment of zero.

SuperCharge and Turbo does not produce any compiled code for a REPEAT statement.


User avatar
dilwyn
Mr QL
Posts: 2753
Joined: Wed Dec 01, 2010 10:39 pm

Re: Loopy bugs

Post by dilwyn »

Martin_Head wrote:
pjw wrote:It appears that Q-Liberator zeroes the loop variable on entry to a loop. The following demo, which is acceptable (although perhaps not very elegant) S*BASIC, will not work in the same way in SuperBASIC or when Qlib compiled.
Yes it does. If you decompile it, you will find a variable assignment of zero.

SuperCharge and Turbo does not produce any compiled code for a REPEAT statement.
Having noticed this in the past and avoided using the "feature" in compiled programs over the years, I'm glad to find it wasn't me doing something wrong after all these years.

Thanks for the explanation. Useful to have this documented.


RichardCGH
Bent Pin Expansion Port
Posts: 81
Joined: Sun Sep 20, 2015 10:07 pm

Re: Loopy bugs

Post by RichardCGH »

The online SuperBasic manual has some interesting comments on FOR loops -
including how different ROMs will process some FOR statements differently.
And some compilers may also have issues.
And SMS.

https://superbasic-manual.readthedocs.i ... F/for.html


User avatar
bwinkel67
QL Wafer Drive
Posts: 1187
Joined: Thu Oct 03, 2019 2:09 am

Re: Loopy bugs

Post by bwinkel67 »

NormanDunbar wrote:Maybe I'm just weird (don't all agree at once!) Or maybe because I used other programming languages, but using a repeat loop name which corresponds to another variable name seems a little daft to me? Happy to discuss if you think otherwise though. I might learn something new.
That's what I was trying to articulate and likely too because I've used other languages.


stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: Loopy bugs

Post by stevepoole »

Hi Bwinkel,

You can use use a specific name for the EXIT variable with a REPeat loop, if you wish.

Indeed, REPeat under SMSQ/E can work without a loop variable ! (Loop variables were retained for backward compatibility).

But the fact is that NEXT never increments REPeat counts, as it would using the FOR variable control list.

NEXT was ONLY retained by Jan Jones in BASIC to allow 'loop epilogues'. (This was why she adopted END loop statements).

In some cases, you can neatly GOTO the END line to increment the FOR variable to avoid the epilogue, (but some programmeurs will get riled up....).

Transcoding QL Basics to Pascal or 'C++' can be very cumbersome where the FOR control lists are complex, (as in EmmBees examples). But Basic is SO easy to program !

So, if you want transcodability, write your programs with REPeat loops only.... Regards, Steve.


User avatar
pjw
QL Wafer Drive
Posts: 1286
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: Loopy bugs

Post by pjw »

First of all lets not get hung up in collateral issues when discussing what are meant to be illustrative examples. A lot of these anomalies were discovered through mistakes or blue sky experimentation, and Im just pointing out what I found, not necessarily endorsing any particular quirk or method. Some parts of the illustrative code may contain now meaningless remnants of the code they were snipped from. Etc, etc. I hope this collectively deals with a number of objections and comments above.


Per
dont be happy. worry
- ?
Post Reply