Positron Compiler Documentation

Do...Loop

Positron8 · Revision 4.0.6.5 · PDF pages 302–303

Do Instructions Loop

or

Do Instructions Loop Until Condition

or

Do Instructions Loop While Condition

Execute a block of instructions until a condition is true, or while a condition is false, or create an infinite 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
MyWord = 1
Do                          ' Create a loop
  HRsoutLn Dec MyWord
  DelayMs 200
  Inc MyWord
Loop Until MyWord > 10       ' Loop until MyWord is greater than 10
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
MyWord = 1
Do                          ' Create a loop
  HRsoutLn Dec MyWord
  DelayMs 200
  Inc MyWord
Loop While MyWord < 11      ' Loop while MyWord is less than 11
View original PDF page 302Open page in PDFOriginal PDF page 302
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
MyWord = 1
Do                            ' Create a loop
  HRsoutLn Dec MyWord
  DelayMs 200
  Inc MyWord
Loop                           ' Loop forever

Do-Loop differs from the While-Wend type in that, the Do loop will carry out the instructions within the loop at least once like a Repeat-Until type, then continuously until the condition is true, but the While loop only carries out the instructions if the condition is true.

Do-Loop is an ideal replacement to a For-Next loop, and can actually take less code space, thus performing the loop faster.

The above example 2 and example 3 show the equivalent to the For-Next loop: -

For MyWord = 1 to 10 : Next

See also : While...Wend, For...Next...Step, Repeat...Until.

View original PDF page 303Open page in PDFOriginal PDF page 303