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 = 18F26K40 ' Tell the compiler what device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Dim MyWord as Word
For MyWord = 0 To 2000 Step 2 ' Perform an upward loop
HRsoutLn Dec MyWord ," " ' Display the value of MyWord
Next ' Close the loop
Example 2
' Display in decimal, all the values of MyWord within a downward loop
Device = 18F26K40 ' Tell the compiler what device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Dim MyWord as Word
For MyWord = 2000 to 0 Step -2 ' Perform a downward loop
HRsoutLn Dec MyWord ," " ' Display the value of MyWord
Next ' Close the loop
Example 3
' Display in decimal, all the values of MyDword within a downward loop
Device = 18F26K40 ' Tell the compiler what device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Dim MyDword as Dword
For MyDword = 200000 To 0 Step -200 ' Perform a downward loop
HRsoutLn Dec MyDword ," " ' Display the value of MyDword
Next ' Close the loop
Example 4
' Display all of Wrd1 using a expressions as parts of the For-Next construct
Device = 18F26K40 ' Tell the compiler what device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Dim Wrd1 as Word
Dim Wrd2 as Word
Wrd2 = 1000
For Wrd1= Wrd2 + 10 To Wrd2 + 1000 ' Perform a loop
HRsoutLn Dec Wrd1," " ' Display the value of Wrd1
Next ' Close the loop
Notes
You may have noticed from the above examples, that no variable is present after the Next command. A variable after Next is purely optional.
For-Next loops may be nested as deeply as the memory on the PICmicro™ will allow. To prematurely exit a loop you may use the Break command.
For Var1 = 0 To 20 ' Create a loop of 21
If Var1 = 10 Then Break ' Break out of loop when Var1 is 10
Next ' Close the loop
BreakOut:
Stop
See also : Bound, Do...Loop, While...Wend, Repeat...Until, SizeOf.

