Code: Select all
Dim i As uByte
Print "Starting For ... Next loop"
For i = 0 To &HFF
Print i,
Sleep 10
Next i
Print "Completed For ... Next loop"
Sleep
Code: Select all
Dim i As uByte
Print "Starting For ... Next loop"
For i = 0 To &HFF
Print i,
Sleep 10
Next i
Print "Completed For ... Next loop"
Sleep
Note: for integer data types, it is not possible to loop up to the highest possible value (or down to the lowest possible value) that can be stored in the variable type, because the loop only breaks when the incremented variable exceeds endvalue, which can never happen. For example, if you try to iterate an variable from 0 to 255, the loop will only break once the variable reaches 256 or more. Using a UByte variable for the counter wouldn't work, because although it can hold the numbers 0 to 255, it cannot hold 256. See Standard Data Type Limits to find the upper and lower limits for the standard data types.
Code: Select all
Dim i As ubyte
do
Print i,
Sleep 10 : i+=1
loop until i=0
Print "Completed do loop"
Sleep
Code: Select all
D:\FreeBASIC>fbc32 --version
FreeBASIC Compiler - Version 1.09.0 (2021-12-31), built for win32 (32bit)
Copyright (C) 2004-2021 The FreeBASIC development team.
standalone
Code: Select all
Dim i As uByte
For i = 0 To &HFF
Print i,
Next i
Code: Select all
fbc32 loop.bas
loop.bas(3) warning 45(1): FOR counter variable is unable to exceed limit value
Code: Select all
width loword(width)+&H2
declare function Roman(as longint) as string
Dim i As uByte
Print "Starting For ... Next loop"
For i = &H0 To &HFF-&H1
Print Roman(i),
Sleep &HA
Next i
print Roman(i)
Print "Completed For ... Next loop"
Sleep
function Roman(num as longint) as string
dim as string m(...) = { "", "M", "MM", "MMM" }
dim as string c(...) = { "", "C", "CC", "CCC", "CD", _
"D", "DC", "DCC", "DCCC", "CM" }
dim as string x(...) = { "", "X", "XX", "XXX", "XL", _
"L", "LX", "LXX", "LXXX", "XC" }
dim as string i(...) = { "", "I", "II", "III", "IV", _
"V", "VI", "VII", "VIII", "IX" }
dim as string thousands = m(num \ 1000)
dim as string hundreds = c((num mod 1000) \ 100)
dim as string tens = x((num mod 100) \ 10)
dim as string ones = i(num mod 10)
dim as string ans = thousands + hundreds + tens + ones
return ans
end function
Code: Select all
dim as long n
dim as ubyte u
print "ubyte","long"
for n =253 to 255
u=n
print u,n
next
print "__________________"
u=n
print u,n
sleep