For Next loops will not terminate at the end of a data type range because overflow is not flagged when the counter variable is stepped prior to the range validity test.
For "Byte" = -128 to 126 works but
For "Byte" = -128 to 127 is an infinite loop because +128 is -128 in Byte.
For "Ubyte" = 0 to 255 is also an infinite loop
If you want to generate all values of a Byte variable you can use a longer datatype and type convert, then assign.
Code: Select all
For variable As Byte = 120 To 126 ' change To 127 and it will infinitely loop
Print variable;" ";
Next variable
Print
sleep
Both Integer and Uinteger do work with negative steps, but if the counter variable is of type Ubyte or Ushort then it fails to run with negative steps. I think it might be because the negative literal step is cast and stored as temporary unsigned. This can be viewed as either incomplete compiler code, a bug or an undocumented restriction.
Code: Select all
print
Print "Loop with Ubyte and negative stepsize"
For variable As Ubyte = 38 To 16 step -1
Print variable;" ";
Next variable
Print
Print
Print "Loop with Ushort and negative stepsize"
For variable As Ushort = 38 To 16 step -1
Print variable;" ";
Next variable
Print
Sleep
For, Next and Step literals appear not to be checked for type range at compile time. You get no warning of type conversion. Here are two examples.
Code: Select all
Print
Print "This will not execute because -130 is +126 as a signed byte"
For variable as byte = -130 To 16
Print variable;" ";
Next variable
Print
' This next executes from 12 to 16
Print
Print "-500 is +12 in Byte so we get"
For variable as byte = -500 To 16
Print variable;
Next variable
Sleep