User Manual
MCP Series
Brushed DC Motor Controllers
MCP Series User Manual
164
IF...THEN...ELSEIF...ELSE...ENDIF
Simple Syntax
if expression then label
if expression then goto label
if expression then gosub label
Extended Syntax
if expression then
...code...
endif
if expression then
...code...
else
...code...
endif
if expression then
...code...
elseif expression
...code...
endif
if expression then
...code...
elseif expression
...code...
else
...code...
endif
Description
IF..THEN commands are the decision makers of MBasic. IF..THEN evaluates a condition to
determine if it is true. If you want to test the value of a variable against another variable or a
known value, you would use the IF..THEN commands. When the results are true, the code after
THEN is executed. If it returns false, the code after THEN will be ignored. A simple example
would be to increment a variable in a loop and each time through the loop test if the variable
equals 10. This lets us control how many times through the loop we want to run. We can also
test if our variable is greater than, less than or not equal too. Several math expressions can be
used for the test condition.
main
if temp = 10 then label
goto main
label
goto main
The above statement is looking for the variable temp to equal 10. If the comparison is true then
we will jump to label. If the comparison is not true, then lets keep looping. Since there is nothing
to make the variable equal to 10 the code snippet would loop forever.