- Name:
- loop
- Syntax:
-
DO
{code}
LOOPDO WHILE/UNTIL condition
{code}
LOOPDO
{code}
LOOP WHILE/UNTIL conditionCondition - The condition which will cause the associated code commands to be executed or not.
Code - Command statements which will be executed dependant upon the evaluation of the condition.
- Description:
-
Indicates the end of a do..loop block, a section of code that will execute continuously, or while or until a condition is met.
Conditions
The condition determine whether the code will be executed or not. Each condition takes the form -variable comparison value The variable's value is compared with the value to determine if the condition is met. For example -
DO WHILE pinC.1 = 0
TOGGLE B.7
LOOPor variable comparison variable The left variable's value is compared with the right variable's value to determine if the condition is met. For example -
DO WHILE b0 < b1
TOGGLE B.7
b0 = b0 + 1
LOOPThe comparison operator may be any of ...
= equal to is equal to <> not equal to != not equal to > greater than < less than >= greater than or equal to <= less than or equal to Note that when testing an input pin, the 'pin' variable name must be used and not the pin number or the pin's port.number label. For example -
DO WHILE pin2 = 1 LOOP UNTIL pinC.0 = 0 If the 'pin' variable name is not specified a 'syntax error' will likely be raised for the 'do' or 'loop' command. Care needs to be taken when specifying an input pin to test using the symbol command.
Multiple conditions
Multiple conditions may be specified in a 'do' or 'loop' conditional command. These can either be 'and' or 'or' combinations -condition AND condition condition OR condition An 'and' combined condition will only evaluate as true when both conditions are true while an 'or' combined condition will evalauate as true when either condition is true.
It is possible to add addditional 'and' or 'or' combinations but it is recommended not to mix both in a combined conditional. For example -
DO WHILE pin2 = 1 AND pin4 = 1 LOOP UNTIL b0 = 2 OR b3 > b4 DO
{code}
LOOPThe code within the do...loop will be continually executed. An exit command can be used to exit out of the do...loop, execution will continue after the 'loop' command.
DO WHILE condition
{code}
LOOPDO UNTIL condition
{code}
LOOPThe code within the do...loop will execute while (or until) the specfied condition is met. An exit command can be used to exit out of the do...loop, execution will immediately continue after the 'loop' command.
DO
{code}
LOOP WHILE conditionDO
{code}
LOOP UNTIL conditionThe code within the do...loop will execute at least once and continule to execute while (or until) the specfied condition is met. An exit command can be used to exit out of the do...loop, execution will immediately continue after the 'loop' command.
A do / loop while structure
Flash an LED 5 times
- Code Example:
-
let b2 = 1 'reset vaiable b2 to 1 do high B.1 'turn on the LED on B.1 pause 1000 'wait 1 second low B.1 'turn off the LED on B.1 pause 1000 'wait 1 second inc b2 'increment the loop counter if pinC.1 = 1 then exit 'exit the loop early if C.1 is high loop while b2 < 5 'otherwise loop
Copy Code Submit an Example
Submit Your Own Code!
You must be logged in to submit code examples. Login now.