Skip to content
Anobium edited this page Oct 18, 2020 · 1 revision

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

    #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

    #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

Handling a FOR-NEXT Overflow

When using FOR-NEXT with Great Cow BASIC you may need to handle an overflow situation.   An overflow will happen when the next step in your increment exceeds the variable type.  

Consider. For = 250 to 255 STEP 3.  The steps will be 250, 253 and 0 etc.   As the third in this sequence has overflowed to 0 not 256 the For-Next loop will continue and you may have unexpected outcomes from your program.  

To resolve this. Do not use a FOR-NEXT but a construct that will ensure an overflow is controlled.  

The following code shows how to use a DO-LOOP to increment (the constant INC) from the base ( MIN) to a maximum value ( __MAX).   This program tests for an overflow condition and will exit, and, the program tests to ensure the next value is less than the maximum.  

Consider. MIN = 250, MAX = 255 and IINC=3.   The steps will be 250 and 253. There is no overflow and the incremented value is always less then the maximum   

    #chip  lgt8fx328p
    #option Explicit

    'USART settings for USART1
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY OFF

    'Wait for terminal to select.. or, give time for me to get to terminal....
    wait 2 s


    '******************************************** START OF CODE ***********************
    #DEFINE __MIN 250
    #DEFINE __MAX 255
    #DEFINE __INC 3
    'This variable needs to be of the correct type Byte, Word etc to handle the maximum value
    dim __nextremetest as byte

    __nextremetest = __MIN

    do
        'Code to show results. Remove from target code
            hserprint __nextremetest
            HSerPrintCRLF
            wait 100 ms
        'Do Stuff
        '...
        'Do Stuff

        __nextremetest = __nextremetest  + __INC
        'Test of overflow
        if c then exit do

        'Now test for maximum has not been exceeded
        if __nextremetest > __MAX then exit do
    loop

This code is efficient in terms coding.   And, this method should be used there is a risk that an overflow situation may happen.

For more help, see Repeat

Clone this wiki locally