Positron Compiler Documentation

Do...Loop

Source: Positron8 Compiler User Manual, PDF page 302

Syntax

Do Instructions

Loop

or

Do Instructions Loop Until Condition

or

Do Instructions Loop While Condition

Overview

Execute a block of instructions until a condition is true, or while a condition is false, or create an infinite loop.

Example 1

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

Example 2

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

Example 3

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

Notes.

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.