Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for-next repeats forever because of data type limit
#4
programandala.net Wrote:
boriel Wrote:Thank you for the explanation. I understand. But at first sight I supposed it would work, because the index values were in range (0-255) for ubyte. You're right, that means 256 iterations, but that's the range a ubyte can hold: from 0 to 255. I supposed the compile time action of NEXT would be to check if the limit (255) had been reached, and don't loop again if so. But it seems it first increases the index. I think the current behaviour is less logical.
In fact, the semantic of FOR is not that. For example:
Code:
FOR i = 0 TO 254 STEP 2:
   PRINT "HELLO"
NEXT i
Will also fail, since 254u + 2u = 0u :!: (Note: 2u => 2 (Ubyte). So here FOR with UByte operates MOD 256!
The meaning for FOR sentence is: "Repeat this loop until (i + step) be GREATER THAN upper limit". Since (i + STEP) is truncated to the current type of variable i (Ubyte in this case), this condition is never satisfied. Keep in mind that FOR can be used with any STEP size (not just STEP 1). In the following version (1.2.6) the compiler might issue a Warning under these circumstances.

I suggest you to try the LOOP workaround code I put above.
Quote:I'll check what FreeBASIC does in such cases.
Please, do, since I'm trying to get close to FreeBasic as much as possible (preserving most of ZX BASIC on the other side ;-))
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)