Dataplot Vol 1 Vol 2

# IF

Name:
IF
Type:
Support Command
Purpose:
Define the start of a conditional block.
Description:
The IF command tests a condition, if the condition is true, execute the following commands until an END OF IF is reached. If the condition is false, skip the following commands.

The following logical operators are used to compare two parameters

 = - equal to > - greater than < - less than <> - not equal to >= - greater than or equal to <= - less than or equal to

The following can be used to test for the existence (or non-existence) of a parameter

 EXISTS - check if the parameter exists NOT EXIST - check if the parameter does not exist

In addition, you can use AND, OR, and XOR to combine testing for two conditions. With AND, the result is true if both conditions are true and false otherwise. With OR, the result is true if either, or both, conditions are true and false if both conditions are false. With XOR, the result is true if either, but not both, of the conditions are true and false otherwise. Currently, only one AND, OR or XOR clause can be used on an IF command. See syntax 3 for details.

The NOT clause can be used to negate the result. That is, if the condition is true, the NOT will result in a false condition. Likewise, if the condition is false, the NOT will result in a true condition.

Syntax 1:
IF <par1> <log> <par2>
where <par1> is a number or parameter;
<log> is a logical operator (=, <>, <, >, <=, >=) that defines the comparison between the two parameters;
and       <par2> is a number or parameter.

That is, this syntax is used to compare two parameters (or numbers) and determine whether the expression is true or false.

Strings can be used with the "=" and "<>" operators, but not the others.

This is most basic and most common use of the IF command.

Syntax 2:
IF NOT <par1> <log> <par2>
where <par1> is a number or parameter;
<log> is a logical operator (=, <>, <, >, <=, >=) that defines the comparison between the two parameters;
and       <par2> is a number or parameter.

This is similar to Syntax 1. The distinction is that it reverses the result. That is, if the <par1> <log> <par2> condition is evaluated as true, the final result is false. Likewise, if the <par1> <log> <par2> condition is evaluated as false, the final result is true.

Strings can be used with the "=" and "<>" operators, but not the others.

Syntax 3:
IF <name> EXISTS
where <name> is the name being tested.

This syntax is used to determine whether <name> exists in the Dataplot name table. It returns true if <name> is found in the name table and false if it is not.

Syntax 4:
IF <name> NOT EXISTS
where <name> is the name being tested.

This syntax is used to determine whether <name> exists in the Dataplot name table. It returns true if <name> is not found in the name table and false if it is found in the name table.

Syntax 5:
IF <par1> <log1> <par2> <AND/OR/XOR> <par3> <log2> <par4>
where <par1> is a number or parameter;
<log1> is a logical operator (=, <>, <, >, <=, >=) that defines the comparison between the two parameters;
<par2> is a number or parameter;
<par3> is a number or parameter;
<log2> is a logical operator (=, <>, <, >, <=, >=) that defines the comparison between the two parameters; and       <par4> is a number or parameter.

The two conditions <par1> <log1> <par2> and <par3> <log2> <par4> are evaluated separately. The two results are then combined based on the AND, OR, or XOR clause.

If the AND clause is used, then the final result is true if both conditions are true and false otherwise.

If the OR clause is used, then the final result is true if either condition, or both, are true and false otherwise.

If the XOR clause is used, then the final result is true if one of the conditions is true and the other is false. If both conditions are true or both conditions are false, the final result is false.

Strings can be used with the "=" and "<>" operators, but not the others.

The AND, OR, and XOR can also be used with Syntax 2, Syntax 3, and Syntax 4.

Currently, only one AND, OR or XOR clause can be included on an IF command.

Syntax 6:
IF COMMAND LINE ARGUMENT <name> EXISTS
where <name> is the name being tested.

Dataplot supports named arguments with the CALL command (see HELP MACRO SUBSTITUTION CHARACTER for details). This syntax is used to determine whether <name> is a currently defined argument on the CALL command.

Examples:
IF A = B
IF A > 2
IF A < 2
IF A <> 2
IF A NOT EXIST
IF A >= 2
IF A <= 2

IF A > 2 AND B < 5
IF A = 2 AND B < 5
IF A = 2 OR B = 6
IF A EXISTS AND A > 1

Note:
IF commands can be nested to a level of 10 deep (this capability was added November, 1992 so earlier versions do not work properly).
Note:
IF blocks can be embedded within LOOP blocks and LOOP blocks can be embedded within IF blocks.
Note:
The IF command has the following limitations.

• There is no ELSE clause with this IF command.

Note: Support for ELSE and ELSE IF clauses was implemented 7/2002. For example,

```IF A = 0
LET B = 0
ELSE IF A > 0
LET B = C/A
ELSE
LET B = 0
PRINT "A < 0"
END OF IF
```

• Expressions are not allowed on the IF. The following code

IF A+B < 10

should be written as

LET C = A+B
IF C < 10

• There is no way to branch out of an IF block from within the block. This is typically only an issue when IF and LOOP blocks are nested.
Note:
If there is an error on the IF command (e.g., one of the parameters is undefined), the IF command returns a status of FALSE.

This was implemented 7/1999. Previous versions left the status undefined.

Note:
You can now test for strings with the IF command. That is,

```
LET STRING S = TEST
IF S = TEST
PRINT S
END OF IF

LET STRING S = TEST
IF S <> "NOT TEST"
PRINT S
ENDS OF IF
```

Note that "=" and "<'>" are the only comparisons allowed (i.e., no "<" or ">").

The argument on the left of the "=" must be the name of a previously defined string. The argument to the right of the "=" is a literal string. The string can be enclosed in dooble quotes if it contains spaces. If there are no double quotes, the string is assumed to end once the first space is encountered.

The 2014/11 version modified this so that the right hand side can now be the name of a previously defined string as well as a literal string.

Default:
None
Synonyms:
None
Related Commands:
 END OF IF = Terminate an IF block. LOOP = Initiate a loop.
Applications:
Program Control
Implementation Date:
Pre-1987 2002/07: Support for ELSE clause
2002/07: Support for testing strings
2014/11: When comparing strings, allow right hand side to be a previously defined string
2016/10: Support for IF COMMAND LINE ARGUMENT ... EXISTS
2016/10: Support for NOT
2016/10: Support for AND, OR, and XOR
Program 1:
```
LET Y1 = NORMAL RANDOM NUMBERS FOR I = 1 1 100
LET A = Y1(10)
IF A < 0
LET Y1(10)=ABS(A)
END OF IF
```
Program 2:
```
let a = 3
let b = 5
.
if a = 3 and b = 5
print "AND Syntax Both True: True"
else
print "AND Syntax Both True: False"
end of if
pause
.
if a = 3 and b = 2
print "AND Syntax A True, B False: True"
else
print "AND Syntax A True, B False: False"
end of if
pause
.
if a = 3 or b = 5
print "OR Syntax Both True: True"
else
print "OR Syntax Both True: False"
end of if
pause
.
if a = 3 or b = 2
print "OR Syntax A True, B False: True"
else
print "OR Syntax A True, B False: False"
end of if
pause
.
if a = 1 or b = 5
print "OR Syntax A False, B True: True"
else
print "OR Syntax A False, B True: False"
end of if
pause
.
if a = 1 or b = 2
print "OR Syntax A False, B False: True"
else
print "OR Syntax A False, B False: False"
end of if
pause
.
if a = 3 xor b = 5
print "XOR Syntax Both True: True"
else
print "XOR Syntax Both True: False"
end of if
pause
.
if a = 3 xor b = 2
print "XOR Syntax A True, B False: True"
else
print "XOR Syntax A True, B False: False"
end of if
pause
.
if a = 1 xor b = 5
print "XOR Syntax A False, B True: True"
else
print "XOR Syntax A False, B True: False"
end of if
pause
.
if a = 1 xor b = 2
print "XOR Syntax A False, B False: True"
else
print "XOR Syntax A False, B False: False"
end of if
```

NIST is an agency of the U.S. Commerce Department.

Date created: 01/27/2017
Last updated: 01/27/2017