Positron Compiler Documentation

Break

Source: Positron8 Compiler User Manual, PDF page 298

Syntax

Break

Overview

Exit a For…Next, While…Wend, Repeat…Until or Do…Loop condition prematurely.

Example 1

' Break out of a For-Next loop when the count reaches 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 Var1 as Byte
  For Var1 = 0 to 39
                                ' Create a loop of 40 revolutions
     HRsoutLn Dec Var1
                             ' Print the revolutions on the serial terminal
     If Var1 = 10 Then Break
                                ' Break out of the loop when Var1 = 10
     DelayMs 200
                                ' Delay so we can see what's happening
  Next
                                ' Close the For-Next loop
  HRsoutLn "Exited At ", Dec Var1 ' Display value when loop was broke

Example 2

' Break out of a Repeat-Until loop when the count reaches 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 Var1 as Byte
  Var1 = 0
  Repeat
                                ' Create a loop
     HRsoutLn Dec Var1
                             ' Print the revolutions on the serial terminal
     If Var1 = 10 Then Break
                                ' Break out of the loop when Var1 = 10
     DelayMs 200
                                ' Delay so we can see what's happening
     Inc Var1
  Until Var1 > 39
                                ' Close the loop after 40 revolutions
  HRsoutLn "Exited At ", Dec Var1 ' Display value when loop was broke

Example 3

' Break out of a While-Wend loop when the count reaches 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 bVar1 as Byte
  bVar1 = 0
  While bVar1 < 40
                                ' Create a loop of 40 revolutions
     HRsoutLn Dec bVar1
                                ' Print the revolutions on a serial terminal
     If bVar1 = 10 Then Break ' Break out of the loop when bVar1 = 10
     DelayMs 200
                                ' Delay so we can see what's happening
     Inc bVar1
   Wend
                                ' Close the loop
  HRsoutLn "Exited At ", Dec bVar1 ' Display value when loop was broke
  Stop

Notes

The Break command is similar to a GoTo but operates internally. When the Break command is encountered, the compiler will force a jump to the loop's internal exit label.

If the Break command is used outside of For…Next, Repeat…Until, While…Wend or Do…Loop, an error will be produced.

If the Break command is used within a Select...EndSelect construct while this is itself inside a loop, only the Select...EndSelect will be exited, not the loop.

See also

Continue, For…Next, While…Wend, Repeat…Until.