For

Syntax:

    For counter = start To end [Step increment]
    ...
    program code
    ...
    <condition> Exit For
    ...
    Next

Command Availability:

Available on all microcontrollers.

Explanation:

The For command is ideal for situations where a piece of code needs to be run a set number of times, and where it is necessary to keep track of how many times the code has run. When the For command is first executed, counter is set to start. Then, each successive time the program loops, increment is added to counter, until counter is equal to end. Then, the program continues beyond the Next.

Step and increment are optionals. If Step is not specified, Great Cow BASIC will increment counter by 1 each time the code is run.

The Exit For is optional and can be used to exit the loop upon a specific condition.

Example 1:

    'This code will flash a green light 6 times.

    #chip 16F88, 8
    #config Osc = Int

    #define LED PORTB.0
    Dir LED Out

    For LoopCounter = 1 to 6
    	PulseOut Led, 1 s
    	Wait 1 s
    Next

Example 2:

'This code will flash alternate LEDS until the switch is pressed.

    #chip 16F88, 8
    #config Osc = Int

    #define LED1 PORTB.0
    Dir LED1 Out
    #define LED2 PORTB.2
    Dir LED2 Out

    #define SWITCH1 PORTA.0
    Dir SWITCH1 In
    main:
    PulseOut LED1, 1 s
    For LoopCounterOut = 1 to 250
    	PulseOut LED2, 4 Ms
    	if switch = On then Exit For
    Next
    Set LED2 OFF
    goto main

For more help, see Repeat