Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for-next repeats forever because of data type limit
#2
programandala.net Wrote:Hi all,

I'm working on my first port from Sinclair Basic to ZX Basic. I'm excited about it. I'd love to program compiled versions of some old programs of mine, but first I need to acquaint with this wonderful and impressive tool.

I'm using ZX Basic 1.2.0 under Debian GNU/Linux.
First of all, 1.2.0 is *VERY* old and might have lot of bugs. Please, download 1.2.5 or 1.2.6-beta here: <!-- m --><a class="postlink" href="http://www.boriel.com/files/zxb/">http://www.boriel.com/files/zxb/</a><!-- m -->

Quote:
Code:
dim b2 as ubyte

for b2=0 TO 255
    print at 2,0;"b2=";b2;"    "
next b2

border 4
print "This message never will be printed"
pause 0

I found the problem because I needed a loop from 0 to 255, so I used the ubyte data type for the index variable. Then I realized the loop repeated forever!
[...]
Do you think this is a language feature or a compiler bug? Smile
Unfortunately, it's a feature. :| This is something already discused: you're iterating a loop 256 times using a byte counter. It's really hard to do this, even in assembler, because you loop over the entire counter range (256), so the count will always overflow. The FOR sentence in basic is very powerful, it's equivalent to the following C code (if you don't know C, it does not matter):
Code:
unsigned char b2;
for (b2 = 0; b2 <= 255; b2++) {
  ...
}
This code will behave the same way: it will never exit. If you want to use a byte, try another loop sentence like DO LOOP UNTIL:
Code:
DIM b2 As Ubyte = 0;

DO
    LET b2 = b2 + 1
    ...
LOOP UNTIL b2 = 0
This should do the Loop using a an Ubyte value (have not tested it yet, btw.)
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)