IF

This command evaluates an expression to determine whether it is True or False, and then it uses this result to execute one of two command strings.

Syntax

IF logical_test, command_if_true, command_if_false

The logical_test parameter will be evaluated for its numeric value. Typical expressions for this parameter are comparative (using >, <, or =), referential (getting the numeric value of a cell reference or a numeric variable), or executive (run a template command that will return a numeric value).

 If the logical_test parameter returns a number greater than 0, the template will execute the command_if_true parameter.

 If the logical_test parameter returns a number less than or equal to 0, the template will execute the command_if_false parameter.

The command_if_true and command_if_false parameters can specify a template command, a value that you want the IF command to return, a blank string (""), or nothing.

 To execute multiple options for either parameter, you may specify a command_if_true or command_if_false parameter of command_1 + command_2.

 You may also specify other IF command strings for the command_if_true and command_if_false parameters.

If the logical_test, command_if_true or command_if_false will execute a command that has optional parameters (like MESSAGE) or unlimited parameters (like CONCAT), you should use parentheses to enclose that command string.

Return value

This command returns the number or string that is returned by the command_if_true or command_if_false parameter that is executed when you run the template.

If nothing is returned by the executed parameter, this command does not return a value.

Notes

To perform multiple logical tests in one IF statement, you can use the Bitand symbol (&) or the Bitor symbol (|) operators in the logical_test parameter. Due to the precedence of the bitAnd and bitOr operators, you must use parentheses around the conditionals being tested.

Note: When using Bitand or Bitor in the logical_test, use caution with comparative commands, which do not always return 1 for "True" and 0 for "False":

  • The Less Than symbol (<) compares two numbers as Number 1 < Number 2 and returns the numeric value of Number 2 - Number 1 .
    If Number 1 is less than Number 2, this return value will be greater than 0 - but it won't necessarily equal 1.

  • The Greater Than symbol (>) compares two numbers as Number 1 > Number 2 and returns the numeric value of Number 1 - Number 2 .
    If Number 1 is greater than Number 2, this return value will be greater than 0 - but it won't necessarily equal 1.

  • The INSTR command returns the starting position of the text string you are looking for.
    If the text string is found, this return value will be greater than 0 - but it won't necessarily equal 1.
    This command does not indicate whether the block of text contains only the text string you are looking for - the text block may contain additional characters.

  • The STRCOMP command compares two strings as STRCOMP "String 1", "String 2" .
    If the two strings match exactly, the return value is 0.
    If String 1 comes before String 2 when sorted in order alphanumerically, the return value is -1.
    If String 2 comes before String 1 when sorted in order alphanumerically, the return value is 1.

One exception is the Equals symbol (=), which always returns 1 for "True" (numbers are equal) and 0 for "False" (numbers not equal).

See Examples below for methods to include comparative commands >, <, and = with Bitand or Bitor in the logical_test.  For string comparisons, you can substitute a STRCOMP command for one of the numbers in an Equals (=) comparison, or you can substitute an INSTR command for one of the numbers in a Less than (<) or Greater than (>) comparison.

Examples

IF ([A1] < 0), (MESSAGE "The number is less than zero."), GOTO [B3]

If cell [A1] is less than 0, display the message. Otherwise, go to cell [B3].

IF ([A1] < 0), "Department A", "Department B"

If cell [A1] is less than 0, return the string value "Department A". Otherwise, return the string value "Department B".

IF ([B1] = 0), "", (MESSAGE "The number is not zero.")

This example performs a "NOT EQUAL" comparison.

If cell [B1] does not equal 0, display the message.

IF DATAOK, GOTO [H1], GOTO [F1]

This formula first executes the DATAOK command.

 If the user clicks Yes, the DATAOK command returns the number 1 and this formula will go to cell [H1].

 If the user clicks No, the DATAOK command returns the number 0 and this formula will go to cell [F1].

IF [D1], GOTO [H1], GOTO [F1]

If the value of cell [D1] is greater than 0, got to cell [H1]. Otherwise, go to cell [F1].

IF MyNVar " OutOfSpec", GOTO [H1], GOTO [F1]

If the value of MyNVar " OutOfSpec" is greater than 0, got to cell [H1]. Otherwise, go to cell [F1].

IF [D1] < 1, (MESSAGE "Please re-enter the value.") + GOTO [D1]

If the value of cell [D1] is less than 1, display the message and go to cell [D1].

IF [G1] = 0, (IF (YESNO "Are you sure you want to Cancel? This will exit the template without saving your data."), EXIT, GOTO [D1])

For the first IF command in this formula (IF [G1] = 0), the command_if_true parameter is another IF command, and no command_if_false parameter has been specified.

For the second IF command in this formula, the template first executes the YESNO command.

 If the user clicks Yes, the YESNO command returns the number 1 and this formula will exit the data entry session.

 If the user clicks No, the YESNO command returns the number 0 and this formula will go to cell [D1].

IF ([A1] = 5) & ([B1] = 10), "Department A", "Department B"

If cell [A1] equals 5 And cell [B1] equals 10, return the string value "Department A". Otherwise, return the string value "Department B".  This uses the Bitand operator.

IF [A1] > 5,  MyNVar "Comparison1" = 1, MyNVar "Comparison1" = 0 :

IF [B1] < 10, MyNVar "Comparison2" = 1, MyNVar "Comparison2" = 0 :

IF MyNVar "Comparison1" & MyNVar "Comparison2", "Department A", "Department B"

If cell [A1] is greater than 5 And cell [B1] is less than 10, return the string value "Department A". Otherwise, return the string value "Department B".  This uses the Bitand operator.

IF (IF [A1] > 5, 1, 0) & (IF [B1] < 10, 1, 0), "Department A", "Department B"

If cell [A1] is greater than 5 And cell [B1] is less than 10, return the string value "Department A". Otherwise, return the string value "Department B".  This uses the Bitand operator.

IF ([A1] = 5) | ([B1] = 10), "Department A", "Department B"

If cell [A1] equals 5 Or cell [B1] equals 10, return the string value "Department A". Otherwise, return the string value "Department B".  This uses the Bitor operator.

IF [A1] > 5,  MyNVar "Comparison1" = 1, MyNVar "Comparison1" = 0 :

IF [B1] < 10, MyNVar "Comparison2" = 1, MyNVar "Comparison2" = 0 :

IF MyNVar "Comparison1" | MyNVar "Comparison2", "Department A", "Department B"

If cell [A1] is greater than 5 Or cell [B1] is less than 10, return the string value "Department A". Otherwise, return the string value "Department B".  This uses the Bitor operator.

IF (IF [A1] > 5, 1, 0) | (IF [B1] < 10, 1, 0), "Department A", "Department B"

If cell [A1] is greater than 5 Or cell [B1] is less than 10, return the string value "Department A". Otherwise, return the string value "Department B".  This uses the Bitor operator.