Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loops with type byte (*solved*)
#1
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?
#2
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.
#3
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*


Forum Jump:


Users browsing this thread: 1 Guest(s)