SED navigation bar go to SED home page go to Dataplot home page go to NIST home page SED Home Page SED Staff SED Projects SED Products and Publications Search SED Pages
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 or more (up to 10) 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.

    Note that the AND, OR, and XOR clause are evaluated pairwise left to right. Currently, the use of parenthesis to group clauses is not supported. For example,

      IF A = 2 AND B = 5 OR C > 10

    is evaluated as follows

    1. The A = 2 part is evaluated and set to either true or false.

    2. The B = 5 part is evalueated and set to either true or false.

    3. The AND operator is applied to these two pieces. So the A = 2 AND B = 5 is set to true if both are true and false otherwise.

    4. The C > 10 part is evaluated and set to either true or false.

    5. The OR operator is then applied to "A = 2 AND B = 5" and "C > 10". If either of these is true, the final result is true. Otherwise the final result is false.

    The following syntax is not supported

      IF A = 2 AND (B = 5 OR C > 10)

    This would need to be coded as

      IF B = 5 OR C > 10 AND A = 2

    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.

    The capability to have more than one AND, OR or XOR clause was added for the 2018/02 version. Up to 10 clauses may be entered.

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. If <name> exists, the IF status is set to true. Otherwise it is set to false.

Syntax 7:
    IF COMMAND LINE ARGUMENT <name> NOT 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. If <name> does not exist, the IF status is set to true. Otherwise it is set to false.

Syntax 8:
    LET <par1> = <expr> IF <par2> <log> <par3>
    where <expr> is an arithmetic expression;
                <par2> is a number or parameter;
                <log> is a logical operator (=, <>, <, >, <=, >=) that defines the comparison between the two parameters;
                <par3> is a number or parameter; and where <par1> is a parameter where the result of <expr> is stored.

    This syntax allows conditional execution of an arithmetic expression. For example,

      LET A = B/A IF A > 0

    Note that this is limited to arithmetic expressions, so commands like the following are not supported

      LET A = MEAN Y IF IFLAG = 1
      LET Y2 = CODE Y IF IFLAG = 1
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

    LET A = B IF B > 0

Note:
    The following

      IF A B

    is equivalent to

      IF A = B

    Although this syntax is allowed, it is recommended that the "=" be included.

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 ">").

    Note: The 2018/10 version added the following

        IF S1 < S2
        IF S1 <= S2
        IF S1 > S2
        IF S1 >= S2

      where S1 and S2 are pre-defined strings. The comparison is based on the ASCII coding sequence (so "A" is less than "a" and "C" is less than "b"). The comparison is performed left to right. If one string is shorter than the other, the shorter string will return 0 for the ASCII code when its length has been exceeded. This syntax does not currently work with literal strings.

    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.

    The 2018/05 version modified this so that the left hand side can supported literal strings. Note that literal strings on the left hand side must be enclosed in quotes. For example, the following are now all supported (S and T are assumed to be pre-defined strings)

      IF S = T
      IF "xxxx" = T
      IF S = "xxxx"
      IF "xxxx" = "xxxx"

    However, be aware that the following may not be interpeted as expected

      IF U = U

    This is not doing a string comparison. It is checking to see if U is the name of a parameter or string. If U does not exist, the IF status is set to false. If U does exist as a parameter or string, the IF status will be set to true. So effectively, this is equilavent to

      IF U EXISTS

    Also, when testing strings, a string containing a single blank character is considered equal to a string with zero characters. That is,

      IF " " = ""

    will evaluate to true.

Note:
    The 2018/05 version added support for

      IF 3 > 2
      IF 3 > A

    That is, the left hand side can now be a number as well as a parameter (previous versions already supported numbers on the right hand side). An example of where this can be useful is

      IF $0 > 0

    The $0 returns the number of command line arguments for a macro. The $0 is evaluated before the IF command is processed, so the IF command would in fact see something like

      IF 2 > 0
Default:
    None
Synonyms:
    None
Related Commands: 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
    2018/02: AND, OR, and XOR extended to up to 10 clauses
    2018/02: For IF A = ..., when A does not exist set IF status to false but do not treat as an error
    2018/05: Support numeric value for left hand side (e.g., IF A > 2)
    2018/05: Support quoted literal string for left hand side (e.g., IF "xxxx" = xxxxx)
    2018/05: Support for IF COMMAND LINE ARGUMENT ... NOT EXIST
    2018/05: Better feedback for special cases
    2018/10: "<", "<=", ">", ">=" support for strings
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
        

Privacy Policy/Security Notice
Disclaimer | FOIA

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

Date created: 01/27/2017
Last updated: 05/15/2018

Please email comments on this WWW page to alan.heckert@nist.gov.