P
ar
t
1: P
rog
ra
m
ming
Moog Animatics SmartMotor™ Developer's Guide, Rev. L
Page 186 of 909
WHILE, LOOP
The most basic looping function is a WHILE command. The WHILE is followed by an expression
that determines whether the code between the WHILE and the following LOOP command will
execute or be passed over. While the expression is true, the code executes. An expression is
true when it is nonzero. If the expression results in a "zero" then it is false. The following are
valid WHILE structures:
WHILE
1
'1 is always true
OS(0)
'Set output to 1
OR(0)
'Set output to 0
LOOP
'Will loop forever
a=1
'Initialize variable "a"
WHILE
a
'Starts out true
a=0
'Set "a" to 0
LOOP
'This never loops back
a=0
'Initialize variable "a"
WHILE
a<10
'a starts less
a=a+1
'a grows by 1
LOOP
'Will loop back 10 times
The task or tasks within the WHILE loop executes as long as the loop condition remains true.
The BREAK command can be used to break out of a WHILE loop, although that somewhat
compromises the elegance of a WHILE statement’s single test point, and makes the code
harder to follow. The BREAK command should be used sparingly or, preferably, not at all in
the context of a WHILE loop.
If it’s necessary for a portion of code to execute only once based on a certain condition, then
use the IF command.
SWITCH, CASE, DEFAULT, BREAK, ENDS
Long, drawn out IF structures can be cumbersome, and burden the program visually. In these
instances it can be better to use the SWITCH structure.
The following code would accomplish the same thing as the ELSEIF program example:
a=IN(0)
'Variable "a" set 0,1
a=a+IN(1)
'Variable "a" 0,1,2
SWITCH
a
'Begin SWITCH
CASE
0
b=1
'Set "b" to one
BREAK
CASE
1
c=1
'Set "c" to one
BREAK
CASE
2
c=2
'Set "c" to two
BREAK
DEFAULT
'If not 0 or 1
d=1
'Set "d" to one
BREAK
ENDS
'End SWITCH
Part 1: Programming: WHILE, LOOP
Содержание SmartMotor
Страница 1: ...Developer s Guide Class 5 Later SmartMotor Technology with TM ...
Страница 909: ...PN SC80100003 002 Rev L ...