Results 1 to 2 of 2

Thread: for next question

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    2

    for next question

    Hi As I'm programming microcontrollers (atmel avr) with the Bascom compiler, I've encounters the next issue:

    Code:
    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?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: for next question

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width