Announcement

Collapse
No announcement yet.

FOR B?=0 TO 255:NEXT 'Invalid range

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bob Zale
    replied
    If you read the definition in the doc, you'll see that loop repeats until the counter variable is greater than the limit. A byte variable can never be greater than 255, so it's illegal.

    This is how For/Next loops have always worked in BASIC.

    Best regards,

    Bob Zale
    PowerBASIC Inc.

    Leave a comment:


  • Kev Peel
    replied
    It only is +1 when the loop is exited normally (ie. after NEXT).

    A BYTE variable can never be greater than 255, the compiler seems to loop from 0-255, so b=256 assigns 0 and b=257 assigns 1 and so on..

    Code:
    Function PBMain
     
      Local i As Long
      Local b As Byte
     
      For i = 255 To 1000
          b = i
          PRINT Str$(i) + Str$(b)
      Next i
      
    End Function

    Leave a comment:


  • Mike Doty
    replied
    'You can actually FOR/NEXT to 255
    'Comes out of loop with B? = 255 and no error, ever.
    'I won't do this, but was curious to see result.
    Code:
    #COMPILE EXE
    #DIM ALL
    FUNCTION PBMAIN AS LONG
      ON ERROR GOTO ErrorTrap
      LOCAL b AS BYTE
      LOCAL High AS BYTE
      High = 255
      FOR b? = 0 TO High  'Invalid For/Next limits
        ? b;
        IF ERR THEN ? "Error" + STR$(ERRCLEAR):SLEEP 2000
        IF b = 255 THEN EXIT FOR
       NEXT
       ? "Out of loop and b?" + STR$(b?)+ "  error code" + STR$(ERRCLEAR)
      
      WAITKEY$
    EXIT FUNCTION
    ErrorTrap:
      ? "Error" + STR$(ERRCLEAR)
      WAITKEY$
    END FUNCTION

    Leave a comment:


  • Mike Doty
    replied
    Thanks, sounds like an optimization.

    Leave a comment:


  • Kev Peel
    replied
    It's probably because the compiler will assign 256 on loop exit. The iteration variable in a FOR loop is always one greater on loop termination (using normal STEP, at least)

    Leave a comment:


  • Mike Doty
    started a topic FOR B?=0 TO 255:NEXT 'Invalid range

    FOR B?=0 TO 255:NEXT 'Invalid range

    Why is 255 invalid in a FOR/NEXT loop of a BYTE?
    Code:
    FUNCTION PBMAIN AS LONG
      LOCAL b AS BYTE
      FOR b? = 0 TO 255  'Invalid For/Next limits
      NEXT
     
      'program around it with this
      b? = 0
      DO
        INCR b?
      LOOP UNTIL b? = 255
     
    END FUNCTION
Working...
X