Download ioCONTROL USER'S GUIDE

Transcript
USING OPTOSCRIPT
Switch or Case Statements
A switch or case statement also offers branching logic and can be used in place of if statements
when the expression can match one of a number of numeric values. The value for each case can
be a numeric constant or a mathematical expression only. Comparisons and logical operators
cannot be used in cases, nor can strings. If a case involves a float, the float is converted to an
integer before use. Notice that only one case can be tested at a time.
Here’s an example of a switch statement.
switch (nNumber)
case 1:
f1 = 10;
break
case 2:
f1 = 15;
break
case (n2 * 2):
f1 = 20;
break
default:
f1 = 0;
f2 = -1;
break
endswitch
The value of the expression in parentheses, nNumber, is compared to each
of the cases. If the case matches the value of nNumber, the action is taken.
Make sure you use a colon (:) after each case.
If a case matches the value of nNumber, the break statement after the
action immediately exits the switch. Notice that a semicolon is not used
after break.
You can use a mathematical expression as a case.
If no case matches, the default action is taken. Using a default is
optional; if you use it, it must be at the end of the list.
A switch statement must be followed by endswitch.
While Loops
The while loop is used to execute a list of statements while a given condition is true. The
condition is tested at the beginning of each loop.
For example, this loop sets the first five elements (elements 0 through 4) of a table (ntTable) to a
value of 10:
nIndex = 0;
while (nIndex < 5)
ntTable[nIndex] = 10;
nIndex = nIndex + 1;
wend
11-22
ioControl User’s Guide
Initialize the counter.
Execute loop if condition is true.
Set the table element.
Increment the counter.