# FOR...NEXT Examples

Syntax: FOR…NEXT

    ' FOR-NEXT.BS1
    ' This example uses a FOR...NEXT loop to churn out a series of sequential
    ' squares (numbers 1, 2, 3, 4... raised to the second power) by using a
    ' variable to set the FOR...NEXT stepValue, and incrementing stepValue within
    ' the loop.  Sir Isaac Newton is generally credited with the discovery of
    ' this technique.

    ' {$STAMP BS1}
    ' {$PBASIC 1.0}

    SYMBOL  square          = B2            ' FOR/NEXT counter
    SYMBOL  stepSize        = B3            ' step size, increase by 2 each loop

    Setup:
      stepSize = 1
      square = 1

    Main:
      FOR square = 1 TO 250 STEP stepSize   ' show squares up to 250
        DEBUG square                        ' display on screen
        stepSize = stepSize + 2             ' add 2 to stepSize
      NEXT                                  ' loop until square > 250
      END

NOTE: The example below is written for the BS2 and will run on any of the BS2-family modules. Modify the $STAMP directive (as required) before downloading to the BS2e, BS2sx, BS2p, BS2pe, or BS2px.

    ' FOR-NEXT.BS1
    ' This example uses a FOR...NEXT loop to churn out a series of sequential
    ' squares (numbers 1, 2, 3, 4... raised to the second power) by using a
    ' variable to set the FOR...NEXT stepValue, and incrementing stepValue within
    ' the loop.  Sir Isaac Newton is generally credited with the discovery of
    ' this technique.

    ' {$STAMP BS1}
    ' {$PBASIC 1.0}

    SYMBOL  square          = B2            ' FOR/NEXT counter
    SYMBOL  stepSize        = B3            ' step size, increase by 2 each loop

    Setup:
      stepSize = 1
      square = 1

    Main:
      FOR square = 1 TO 250 STEP stepSize   ' show squares up to 250
        DEBUG square                        ' display on screen
        stepSize = stepSize + 2             ' add 2 to stepSize
      NEXT                                  ' loop until square > 250
      END

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012