IfThen

(Engine-Level Function)

Description:

Conditionally execute statements.

This statement executes all provided statements or expressions if the condition parameter is true.

Returns: Nothing
Usage: Script only.
Function Groups: Logic Control
Related to: Cond | Execute | IfElse
Format: IfThen(Condition, Expression1, Expression2, ...)
Parameters:  
Condition   
Required. An expression that will indicate whether the Expression parameters are executed. If this is true, the Expression parameters will be executed.
Expression1, Expression2, ...
Required. Any statements to be executed when Condition is true. Any number of Expression parameters may be listed. They are executed once in the order that they are listed.
Comments: This statement may only appear in a script.

Example:

  If 1 Main;
  [
    IfThen(Valid(TankPtr) && FluidLevel > 1000, 
      StartPump = 1; 
      ActiveTankNum++; 
      Msg = "Changing tanks..."
    );  
  ]

The statement in the script will test to see if TankPtr exists (is valid) and if FluidLevel > 1000. If these conditions are both true, the series of actions will be taken.

Note that VTScada does not use short-circuit evaluation. Both parts of the condition will always be checked even if TankPtr is Invalid. Do not write code similar to the following:

    IfThen(Valid(Tank) && (Level = Tank.FluidLevel) > 1000,
     ...
    );

Since we do not do short-circuit evaluation of Boolean expressions, Level always gets set, even when Tank is Invalid. In that case, Level will be set to Invalid, which may not be appropriate. Solve this by nesting the code:

    IfThen(Valid(Tank),
      IfThen(Level = Tank.FluidLevel) > 1000,
       ...
      );
    );

Note that the Trihedral code style recommends doing the Level assignment before the nested IfThen, and then simply checking Level in the nested IfThen's condition.