Crestron
SIMPL+
Software
while and do-until Loops
The
for
loop discussed in an earlier section is useful for iterating through code a
specific number of times. However, sometimes the exact number of times a loop
should repeat is unknown. Instead, it may be necessary to check to see if a condition
is true after each loop to decide whether or not the loop should execute again.
There are two looping constructs in SIMPL+ which allows execution of a loop only
for as long as a certain condition is true. These are the
while
and
do-until
loops. The
while loop has the following syntax.
while (expression)
{
<statements>
}
When the
while
statement is executed, the expression contained in parentheses is
evaluated. If this expression evaluates to TRUE, then the statements inside the braces
are executed. When the closed brace is reached, the program returns to the
while
statement and reevaluates the expression. At this point the process is repeated. It
should become clear that the code inside the braces is executed over and over again
as long as the
while
expression remains TRUE.
The nature of the
while
loop means that it is the responsibility of the programmer to
ensure that the loop is exited at some point. Unlike the
for
loop discussed previously,
this loop does not run for a set number of times and then finishes. Consider the
example after this paragraph.
x = 5;
while (x < 10)
{
y = y + x;
print("Help me out of this loop!\n");
}
Endless loops cause the
SIMPL+ module (in which
they occur) to rerun the same
code forever. However, due
to the multi-tasking nature of
the operating system, an
endless loop in one module
does not cause the rest of the
SIMPL program (including
other SIMPL+ modules) to
stop running. This is
discussed in more detail in
“Understanding Processing
Order” on page 49.
This example shows an endless loop. That is, a loop that runs forever and never
exits. The problem is that the value of the
while
expression never changes once the
loop is entered. Thus the expression can never evaluate to FALSE. Include code
inside the loop that affects the
while
expression (in this case the variable
x
must be
modified in some way) and allows the loop to exit at some point.
The
do-until
looping construct is similar to the
while
loop. The difference lies in
where the looping expression is evaluated and how this evaluation affects the loop.
To see the difference, examine the form of a
do-until
loop.
do
{
<statements>
} until (expression)
From the syntax, it is obvious that the looping expression for a
do-until
appears after
the looping code. This expression appears before the code in a
while
loop. This
discrepancy affects the way the loop is initially entered. As was shown above, a
while
loop first evaluates the expression to see if it is TRUE. If it is, then the loop
runs through one time and then the expression is evaluated again.
The
do-until
loop differs from this in that it always executes at least one time. When
the
do
keyword is reached, the code that follows (enclosed in braces) is executed
before the value of the
until
expression is evaluated. After the initial pass through
Programming Guide – DOC. 5789A
SIMPL+
•
29