Forum
For loops with type byte (*solved*) - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Bug Reports (https://www.boriel.com/forum/forumdisplay.php?fid=15)
+---- Thread: For loops with type byte (*solved*) (/showthread.php?tid=127)



For loops with type byte (*solved*) - britlion - 08-17-2009

Code:
dim c,b as byte


FOR c=1 TO 175
FOR b=1 TO 250
  PLOT b,c
NEXT b
NEXT c

Doesn't seem to work as expected. If I make it a uinteger, it works perfectly. At first I thought it was a problem with 0=256 in a byte the original was
Code:
FOR c=0 TO 175
FOR b=0 TO 255

Which I could see not terminating, since a byte can never get bigger than 255.

However the problem I have with it as written is it's skipping the loop altogether..

1> Would for/next loops with byte be faster? (I'm following the principle of smaller=faster)

2> Why isn't it running the loop?


Re: For loops with type byte - boriel - 08-17-2009

britlion Wrote:
Code:
dim c,b as byte


FOR c=1 TO 175
FOR b=1 TO 250
  PLOT b,c
NEXT b
NEXT c

Doesn't seem to work as expected. If I make it a uinteger, it works perfectly.
You must use ubyte (Unsigned Byte), since byte type is signed, and in the above lines, you have an overflow:
ubyte 175 is -85 byte => the FOR loop is skipped. In the future, the compiler might issue an overflow warning in these cases to warn you about this.
Quote:1> Would for/next loops with byte be faster? (I'm following the principle of smaller=faster)
It should be. Z80 is an 8 bit machine and the FOR is done with 8bit values when using bytes.
If you can understand Z80 ASM (I guess you do Wink) compile your program with --asm and have a look at the output.


Re: For loops with type byte - britlion - 08-19-2009

Oh dear. :oops:

For some reason I was thinking of byte as unsigned, 0-255. *bangs head on desk* Ubyte. Ubyte.

Thank you for that. It's obvious now why it didn't work.

I feel like such a noob.

*slinks away*