IF Command
Purpose
Tests a condition and then specifies a statement number to go to.
Syntax
IF expression GOTO step #
Parameters
expression – compares values in the IF command. The syntax of an expression for these commands is:
operand1 relationaloperator operand2
For more information on these arguments, refer to the topic Using Command Arguments.
GOTO step# – specifies the step number you want to go to when the expression is TRUE. If the designated step number is less than the current step number (making a backward jump), iFIX suspends processing the Program block and the jump occurs during the next scan period. If the designated step number is greater than the current step number (making a forward jump), execution continues at the next step without interruption.
TIP: In rare instances, you can have a Program block that waits for a specific value from a block to test a condition, but because of the unavoidable round-off errors that occur in digital systems, the block consistently sends out a slightly different value that never satisfies the condition. For example, 10 might not equal 10, since the block might really be comparing 10.0001 to 10.0003. The easiest way around this problem is to add a SETLIM command to create a limit of precision on the value that is received from the block. This command lets you assign an inclusion limit for the comparison, such as +/- 0.1.
Examples
You can use the IF command to test for a condition and then jump to a specific command. For example, if the value of the Analog Input block, LEVEL, is greater than 3000, then proceed to step 12 uses the following statement:
08 IF LEVEL > 3000 GOTO 12
Operands
The IF command can also use the operands:
- DATE
- DAY
- TIME
IF DATE
To test that the date is June 29, 1997 before executing step 12, enter:
04 IF DATE > 06-29-97 GOTO 12
If the date is any other date, then step 05 executes.
IF DAY
To test for the correct day, use the following statements:
Step# |
Command |
Action |
3 |
IF DAY=SUN GOTO 06 |
If it is Sunday, go to Step 6. |
4 |
WAITFOR TIME =23:59:59 |
If it is not Sunday, wait = 23:59:59 until the next day. |
5 |
GOTO 3 |
Check again to see if it is Sunday. |
6 |
CALL PM |
Call Program block for preventive maintenance cycle. |
NOTE: You can only use = and != for operators with the DAY operand. Legal identifiers for days of the week are SUN, MON, TUE, WED, THU, FRI, and SAT.
IF TIME
The TIME operand executes the designed step number according to the time of day. This command differs from the IFTIME GOTO command in that you do not have to use it in conjunction with a WAITSTAT command. To make the distinction between the commands, use a space between the IF command and the TIME operand.
The IF TIME command uses the block's scan time as a +/- dead band if you are using the Equals (=) operator. You can enter the time in a 24-hour format, HH:MM:SS.
Consider the following command:
07 IF TIME = 12:00:00 GOTO 4
If the block has a scan time of 2 minutes, the command evaluates to true at any time between 11:58:00 and 12:02:00 as shown below.
Therefore, even if the block is not scanned at exactly 12:00:00, the IF TIME command still executes as close to 12:00 as the scan time allows.
However, because of the deadband, it is possible for the IF TIME command to execute more than once. For example, if iFIX scans the block with the - scan time to the + scan time range, it is scanned twice, as shown below:
If the block is scanned exactly on the edge of the deadband, iFIX scans the block three times, as shown below:
To prevent multiple executions, the IF command could be used with TIME and GOTO as follows:
Step# |
Command |
Comments |
14 |
IF TIME=12:00:00 GOTO 16 |
12:00 is half an hour prior to plant shutdown; start shutdown procedures. |
15 |
WAITFOR TIME =23:59:59 |
If time is outside the deadband range, return to step 0 and start another run. |
16 |
GOTO 3 |
Reminder to operator to start shutdown procedures. |
17 |
CALL PM |
Delay for three times the scan time (2 minutes in this case) to prevent the IF TIME command from executing more than once. |