For...Next...Step
Syntax
For Variable = Startcount to Endcount [ Step { Stepval } ] {code body}
Next
Overview
The For…Next loop is used to execute a statement, or series of statements a predetermined amount of times.
Parameters
Variable refers to an index variable used for the sake of the loop. This index variable can itself be used in the code body but beware of altering its value within the loop as this can cause many problems. Startcount is the start number of the loop, which will initially be assigned to the variable. This does not have to be an actual number - it could be the contents of another variable. Endcount is the number on which the loop will finish. This does not have to be an actual number, it could be the contents of another variable, or an expression. Stepval is an optional constant or variable by which the variable increases or decreases with each trip through the For-Next loop. If Startcount is larger than Endcount, then a minus sign must precede Stepval.
Example 1
' Display in decimal, all the values of MyWord within an upward loop
Device = 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
Declare Hserial_Baud = 9600 ' USART1 Baud rate
Declare Hrsout1_Pin = PORTB.14 ' Select the pin for TX with USART1
Dim MyWord as Word
RPOR7 = 3
' Make PPS Pin RP14 U1TX
For MyWord = 0 to 2000 Step 2
' Perform an upward loop
Hrsout Dec MyWord, 13
' Display the value of MyWord
Next
' Close the loop
Example 2
' Display in decimal, all the values of MyWord within a downward loop
Device = 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
Declare Hserial_Baud = 9600 ' USART1 Baud rate
Declare Hrsout1_Pin = PORTB.14 ' Select the pin for TX with USART1
Dim MyWord as Word
RPOR7 = 3
' Make PPS Pin RP14 U1TX
For MyWord = 2000 to 0 Step -2
' Perform a downward loop
Hrsout Dec MyWord, 13
' Display the value of MyWord
Next
' Close the loop
Example 3
' Display in decimal, all the values of MyDword within a downward loop
Device = 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
Declare Hserial_Baud = 9600 ' USART1 Baud rate
Declare Hrsout1_Pin = PORTB.14 ' Select the pin for TX with USART1
Dim MyDword as Dword
RPOR7 = 3
' Make PPS Pin RP14 U1TX
For MyDword = 200000 to 0 Step -200 ' Perform a downward loop
Hrsout Dec MyDword, 13
' Display the value of MyDword
Next
' Close the loop
Example 4
' Display all of MyWord1 using expressions as parts of the For-Next
Device = 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
Declare Hserial_Baud = 9600 ' USART1 Baud rate
Declare Hrsout1_Pin = PORTB.14 ' Select the pin for TX with USART1
Dim MyWord1 as Word
Dim MyWord2 as Word
RPOR7 = 3
' Make PPS Pin RP14 U1TX
MyWord2 = 1000
For MyWord1= MyWord2 + 10 to MyWord2 + 1000
' Perform a loop
Hrsout Dec MyWord1, 13
' Display the value of MyWord1
Next
' Close the loop
Notes.
It may have been noticed from the above examples, that no variable is present after the Next command. A variable name after Next is purely optional.
For-Next loops may be nested as deeply as the code memory on the microcontroller will allow. To break out of a loop you may use the GoTo command without any ill effects, which is exactly what the Break command does: -
For MyByte = 0 to 20
' Create a loop of 21
If MyByte = 10 Then GoTo BreakOut
' Break out of loop when MyByte is 10
Next
' Close the loop
BreakOut: