PDA

Click to See Complete Forum and Search --> : for next question


kolonelvonklink
May 31st, 2007, 04:56 AM
Hi As I'm programming microcontrollers (atmel avr) with the Bascom compiler, I've encounters the next issue:

Dim x as byte
For x = 0 to 20
print x
next
print x
end

as result I get the printing of "0" to "21"

seems confusing/unlogical to me since I asked to go to 20 and not to 21

Is this also case in other basics?

si_the_geek
May 31st, 2007, 08:27 AM
I have never used the compiler you are using, but this is normal behaviour for many languages (including several/all versions of Basic). When a For loop ends, the loop variable is 1 higher than the "to" value you specified.

If you remove the second Print statement, you will not see this issue - you will only get 0 to 20 shown.