The statements in this section allow you to conditionally execute blocks of statements, loop repeatedly through blocks of statements, and start execution of other sequences.
Several AIM statements allow expressions to be entered in their argument lists. For example: the SET, SETS, IF, and WHILE statements all accept expressions.
AIM expressions are combinations of operators, variables, and constants. No parentheses are allowed, so the precedence of the operators determines the value of the expressions.
The unary operators ("-", "COM", "NOT") always have the highest precedence. Therefore, expressions like:
are evaluated as (NOT a) AND b.
Each binary operator has its own precedence. Higher precedence means that the operations occur first. If operators have the same precedence, they are evaluated from left to right.
| Operators |
Precedence |
|---|---|
| * / |
10 |
| + - |
9 |
| < > =< >= |
8 |
| == <> |
7 |
| BAND BAND_COM |
6 |
| BXOR |
5 |
| BOR BOR_COM |
4 |
| AND AND_NOT |
3 |
| XOR |
2 |
| OR OR_NOT |
1 |
a > b AND c > d OR a+b < c
((a > b) AND (c > d)) OR ((a+b) < c)
Conditional and looping structures allow you to specify under what conditions and how many times blocks of statements will execute. Conditional and looping structures may be very simple and straightforward or they can use complex arithmetic expressions and relational logic. The examples in the following sections present the most common uses of the various structures. A complete understanding of the complex ways that the structures can be used requires that you understand relational logic. (The V+ Language User's Guide describes the basics of relational logic.)
All the structures share the following common attributes:
The block of statements that will be executed begins immediately after the
structure statement and ends with an END statement. There must be an END
statement for each control structure statement; multiple control structures
cannot be ended with a single END statement.
Structures may be nested within each other.
The variables used in a looping statement (FOR, REPEAT) should never be
changed by any statements within the loop. Also, during execution of the
loop, any other programs that might have access to global variables used in
the looping statement should not change those variables.
In all computer systems, numbers are internally represented in binary format.
This internal format may involve some rounding of the actual number since
the internal representation may not exactly represent base 10 numbers. This
internal representation can lead to unexpected behavior when comparing real
values (such as 3.1467) because two numbers that appear identical in the
display may have different internal representations. When using variables in
statements that test for equality, use integer values to avoid this potential
problem.
The CALL statement allows you to call one sequence from within another sequence. When a CALL statement is encountered, the currently executing sequence (calling sequence) will suspend operation and the new sequence (called sequence) will begin. When the called sequence completes, the calling sequence will resume execution at the next statement line.
Sequence calls can be nested, and a called sequence itself can call another sequence. The Stack button on the Task Control Panel will show you how many calls have been made and the point in the sequence that the calls were made. Only sequences in the assigned module can be executed using the CALL statement.
The CASE structure is similar to the IF...THEN...ELSE statement except that it can conditionally execute any of several different blocks of code based on the value of a variable. A CASE structure is actually built from the CASE, VALUE, ANY, and END statements. In the following example, if the variable code has a value of 1, STATEMENT one will execute. If it has a value of 2, STATEMENT two will execute. If it has any other value, STATEMENT three will execute.
An ELSE statement is always used in conjunction with an IF statement. The ELSE statement specifies a block of statements to execute when the statements specified by the preceding IF statement are not executed. The ELSE statement can also specify conditions to control execution of statements within the ELSE block.
The syntax of the ELSE statement is:
ELSE {IF {--uopr--} --variable-- {--opr--
--variable-- {--opr-- --variable-- {--opr-- --variable--
{--opr-- --variable-- {--opr-- --variable--}}}}}}
In the following example, statement 2 will execute if x has a value of 7, and statement 4 will execute if it has any other value.
In the following example, statement 2 will execute if x == 7, and statement 4 will execute if x has any other value and y == 3. If both x and y do not have the specified values, neither statement will execute.
A statement block is terminated with an END statement. In the following sequence, the first statement will execute, statements 3 and 4 will execute only if x has a value of 7, and statement 6 will execute normally.
An EXIT_LOOP statement will cause a REPEAT, FOR, or WHILE loop to terminate and sequence execution to continue at the next statement following the END statement that closes the REPEAT or WHILE loop.
The syntax for an EXIT_LOOP statement is:
EXIT_LOOP {--constant--}
This optional argument specifies the number of loops to exit. If an EXIT_LOOP is within a REPEAT loop that is nested within a WHILE loop, specifying 2 for the --constant-- argument will result in terminating the execution of both loops.
An EXIT_LOOP usually contains IF/END statements, which identify a condition that dictates when a loop should be exited early. In the following example, a REPEAT loop is started that will normally execute 10 times. Halfway through the loop, variable x is checked. If it has a value of 7, the repeat loop is exited and the sequence resumes execution at statement 8.
The FOR statement allows you to execute a block of statements a specified number of times.
FOR --o_variable-- = {--uopr--} --variable-- {--opr--
--variable--} TO {--uopr--} --variable-- {--opr-- --variable--} {STEP --constant--}
FOR statements can become complex. Following are several examples beginning with simple FOR loops and proceeding to complicated FOR loops.
The following example executes a block of code four times:
FOR loop_var = 1 TO 4
In the above example, loop_var (which must be an AIM variable, V+ variable or ai.ctl[ ] value in the Variables database) will increment once each time the loop is executed. If any other statements within the loop access the variable loop_var, the variable will have the value of the current cycle of the loop.
You can make the FOR loop "count by twos" by including the STEP argument:
FOR loop_var = 2 TO 4 STEP 2
The above example would execute twice with loop_var having the value of 2 and 4 during successive loops.
You can count from a negative number by using the --uopr-- argument:
FOR loop_var = -2 TO 4 STEP 2
Double-clicking --uopr-- will give you the option of "-", NOT, or COM. The "-" is the only selection that would normally be used in a FOR statement. You also can make the FOR statement count backwards by giving the STEP argument a negative value (note the relationship of the "=" and TO argument values):
FOR loop_var = 4 TO -2 STEP -1
You can make the "=" and TO argument values rely on a calculation by using the
--opr-- and --variable-- arguments:
FOR loop_var = 4 * x TO -2 STEP -1
The GOTO statement allows you to jump to any other labeled step in a sequence.
GOTO --string-- {IF {--uopr--} --variable-- {--opr--
--variable-- {--opr-- --variable-- {--opr-- --variable--
{--opr-- --variable-- {--opr-- --variable--} } } } } }
NOTE: Beginning with AIM version 4.2 edit C4, the GOTO statement has an optional IF section that allows you to conditionally skip to a label. If the expression returns TRUE, the GOTO will jump to the label, otherwise it will continue execution at the next statement. It is equivalent to:
IF expression
GOTO label
END
You create a step "label" by typing any 15-character label on a line displaying the
--statement-- place holder. The label must begin with a character and must not include any spaces. It can contain mixed alpha and numeric characters. The label must be terminated with a colon ":". In the following example, if the variable check has a value of 7, a GOTO will be executed and execution will jump to line 15 of the sequence.
The IF statement allows you to conditionally execute a block of statements.
IF {{--uopr--} --variable-- {--opr-- --variable-- {--opr--
--variable-- {--opr-- --variable-- {--opr-- --variable--
{--opr-- --variable--}}}}}}
To execute a block of statements if a variable has a certain value, specify the variable in the first --variable-- clause, select "==" as the first --opr--, and specify the required value as the second --variable--:
IF x == 3
If you want to execute a block of statements based on the value of two variables, you can use one of the following statements:
IF x == 3 AND y == 7 IF x == 3 OR y == 7
The first example executes the statement block if x has a value of 3 and y has a value of 7-both variables must have the indicated values. In the second example, if either variable has the indicated value, the statement block will execute.
To create a conditional block that depends on the state of a digital signal, select a variable that has Input signal or Output signal selected:
IF in_sig IF NOT in_sig
In the first case, the statement block will execute if the signal is on. In the second case, it will execute if the signal is off.
The NEXT_LOOP statement is similar to the EXIT_LOOP statement. However, execution of the looping block is not canceled completely: Only the current iteration of the loop is canceled. In the following example, if y is less then 10 when statement 4 is reached, the sequence will resume execution at statement 1:
The REPEAT statement allows you to repeat a block of statements a given number of times. The arguments to REPEAT allow you to evaluate a complex expression when deciding how many times to repeat a block of statements.
REPEAT {--uopr--} --variable-- {--opr-- --variable--
{--opr-- --variable-- {--opr-- --variable-- {--opr--
--variable-- {--opr-- --variable--}}}}}
To simply repeat a block of statements a given number of times, specify that number for the first --variable--:
REPEAT 4
To repeat a loop four times plus the value of the variable x, select "+" as --opr-- and the record x as the second --variable--:
REPEAT 4 + x
The block of statements included in a REPEAT loop is identified by an END statement. In the following example, statements 2 through 6 will be executed five times.
The RETURN statement causes the current sequence to stop executing as if the end of the sequence had been reached and all cycles had been completed. If this sequence was initiated by a CALL statement, the sequence that executed the CALL statement resumes execution at the statement following the CALL. If the CALL statement was executed from the main sequence, the runtime task returns to idle state.
RETURN
The SET statement allows you to assign a new value to a variable.
SET --o_variable-- = {--uopr--} --variable-- {--opr--
--variable-- {--opr-- --variable-- {--opr-- --variable--
{--opr-- --variable-- {--opr-- --variable--}}}}}
In the following example, the SET statement is used to assign a new value to var1.
SET var1 = var2 + var3 - 12
The SETS statement allows you to assign a new value to a string variable.
SETS --o_variable-- = {--s_uopr--} --variable--
{--s_opr-- --variable-- {--s_opr-- --variable--
{--s_opr-- --variable--{--s_opr-- --variable-- {--s_opr--
--variable--}}}}}
The following binary and unary operators (shown in bold type) may be used for the statement argument --s_opr--
The WHILE control structure causes a block of statements to be indefinitely executed as long as the specified conditions are correct.
The syntax for the WHILE statement is:
WHILE {--uopr--} --variable-- {--opr-- --variable--
{--opr-- --variable-- {--opr-- --variable-- {--opr--
--variable-- {--opr-- --variable--}}}}}
In the following example, statements 2 and 3 will execute only if signal in_sig1 is on or signal in_sig2 is off when statement 1 is encountered. They will continue to execute as long as in_sig1 is on or in_sig2 is off: