PIC32 Looping & Branching

Looping & Branching (Program Flow)

if, then, else, endif for, to, next, step while
wend

breakif
select, case, default, endselect label
goto
exitf
exitif

There are six methods of controlling a program as can be seen from the table above. They follow what you would expect from a BASIC syntax.

if-then
if <condition> then [else] endif
if a$>”fred” then
    x=1
endif

if b=3 then
    some code ...
else
    some code..
endif

This statement works by testing for a condition that works on float, integer and string types. the conditions are:

Condition Description
= Equals
> Greater then
< Less then
>= Greater then or equal to
<= Less then or equal to
<> Not equal


There must be a condition following the subject variable or constant otherwise an error will occur. If a variable is used in the condition statement it must already exist. A condition may compare any variable type.

for - next
for <start> to <end> [step <value>]
dim j,fred#
for j=1 to 6
    some code..
next

for fred#=1.3 to top# step 0.1 some code.. next

The variable used for looping must be created with a 'dim' prior to use.

while - wend
nwhile <condition> [breakif] wend
while k<8
    some code..
    breakif k<0
wend

This is the normal while loop with a variation using break if. If the condition following breakif is true then the loop will terminate.

select - case
select <variable> case <value> [default] endselect
select j
    case 1
        some code...
        some code..
    case 2
        some code..
    default
        some code
endselect

A variable must follow ‘select’, and should have already been defined. A comparison is made with the value following the ‘case’ statement for equality. If it is equal the code is run and execution continues after the ‘endselect’ statement. If no match can be made with any of the case statements then the code is run following the ‘default’ statement. The ‘default’ statement is optional and can be left out of the code if required. Matches will work with Integer, Hex, Float or String.

goto - label
ngoto <label>
function test
    code...
    goto er1
    code...
label er1
    code...
endf

This will allow a jump to a predefined label WITHIN a function. Jumps can only be forward but can exist even if inside a loop. This is a very useful statement if for example there are several options within a ‘function’ but on error some exit code needs so be executed, ‘closefile’ is a good example. A ‘goto’ can only be made within a function, it is not possible to jump out of a function using ‘goto’.

exitf
exitf

This can be used at any point within a function. It will immediately leave the function as ‘endf’.

breakif
breakif <condition>
breakif 1=1
breakif a>7

Break if can be used within a looping structure ‘while-wend’ or ‘for-next’ and provides a method of early exist should the condition be met.

exitif
exitif <condition>
exitif 1=1
exitif a>7

This is the same as 'exitsub' but will test the condition following the statement and exit only if it is true.