Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for-next repeats forever because of data type limit
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.

Well, I think I found a kind of bug. Here you are the test:

' The following loop works as expected:

dim b0 as ubyte

for b0=0 TO 254
    print at 0,0;"b0=";b0;"    "
next b0

' The following loop works as expected too:

dim b1 as uinteger

for b1=0 TO 255
    print at 1,0;"b1=";b1;"    "
next b1

' But the following loop starts again forever:

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!

I guess the reason is NEXT increases the loop index before cheking it, so 256 becomes 0 after storing it back into the ubyte variable and the check gets wrong: the loop starts again because the index is zero! I guess the same happens with other data types, as long as their limits are reached, but I didn't try.

Do you think this is a language feature or a compiler bug? Smile

I'm programming a project in FreeBASIC too, but I didn't try this. I'll try a similar code and I'll see what happens.

Messages In This Thread

Forum Jump:

Users browsing this thread: 1 Guest(s)