Control structures (if, while) – MTS Series 793 User Manual

Page 379

Advertising
background image

No further calculation commands are evaluated in the calculation until it is reset

A message is written to the station log stating that the calculation was aborted due to a user generated
error condition

The error condition is written to the station log

Control Structures (if, while)

Expressions support two control structures—the if statement, and the while statement.

if

if statements work similar to their equivalents in the C language:

if (expression)

if (expression)

trueStatement; else
falseStatement;

if (expression)

trueStatement;

{

statements;

}

The expression is something that evaluates to an integer. Zero means false, and non-zero means true. Most
often, this expression will involve relational operators, and logical operators.

For example, the following code, where Maximum and Minimum are calculation parameters, clips the data
on “My Signal” and puts the result on “My clipped signal.”

if (“My Signal” > Maximum)

“My clipped signal” = Maximum;

else if (“My Signal” < Minimum)

“My clipped signal” = Minimum;

else

“My clipped signal” = “My Signal”

To put more than one statement into the true or false conditional, you can use a compound statement. A
compound statement is a series of statements enclosed with { } (called curly brackets or braces).

while

The while loop has a similar form:

while (expression)

while (expression)

{

statement:

statements;

}

The following example implements a 50-tick delay. It also contains logic that will initialize the array the first
time it is called. After the first time, the variable “i” will contain 50, so the logic will not be repeated.

real oldValues[50];
int i;
while (i < 50)
{

oldValues[i] = “My Signal”;
i = i + 1;

}
“Delayed Signal” = shift(oldValues, “My Signal”);

MTS Series 793 Control Software 379

Calculated Signals

Advertising