Returns one value if a condition is true, and returns another value if the same condition is false.

Description

The If function tests conditions until a true result is found. The corresponding value is then returned as the result. You can use If to return different results based on comparisons and other tests.

You can use If in behavior formulas to branch between two actions. At most, one branch will be executed. Conditions are evaluated in order, and after a true result is found, no further conditions are checked.

If no conditions are satisfied and an odd number of arguments are provided, the value of the last argument is returned. This is the case with the common If( Condition, Value, Else ). If an even number of arguments are provided, Blank is returned.

Syntax

If( Condition, Result [, ElseResult ] )
If( Condition1, Result1 [, Condition2, Result2, ... [ , ElseResult ] ] )

  • Condition(s) - Required. Formulas to test for true. These formulas commonly contain comparison operators (such as <, >, and =) and test functions such as IsBlank and IsEmpty.
  • Result(s) - Required. The corresponding value to return for a condition that evaluates to true.
  • ElseResult - Optional. The value to return if no conditions are satisfied. If you don't specify this argument, blank is returned.

Examples

Values in formulas

In the following examples, a slider control named Slider1 has a value of 25.

Formula Description Result
If( Slider1.Value = 25, "Result1" ) The condition is true, and the corresponding result is returned. "Result1"
If( Slider1.Value > 1000, "Result1" ) The condition is false, and no ElseResult is provided. blank
If( Slider1.Value = 25, "Result1", "Result2" ) The condition is true, and the corresponding result is returned. "Result1"
If( Slider1.Value > 1000, "Result1", "Result2" ) The condition is false, and the ElseResult has been provided and is returned. "Result2"
If( Slider1.Value = 25, "Result1", Slider1.Value > 0, "Result2" ) The first condition is true, and the corresponding result is returned. The second condition is also true, but its corresponding value isn't returned because it appears on the argument list after the first condition. "Result1"
If( IsBlank( Slider1.Value ), "Result1", IsNumeric( Slider1.Value ), "Result2" ) The first condition is false because the slider has a value of 25 and isn't blank. The second condition is true because the slider's value is a number, and the corresponding result is returned. "Result2"
If( Slider1.Value > 1000, "Result1", Slider1.Value > 50, "Result2", "Result3") Both the first and second conditions are false, and the ElseResult has been provided and is returned. "Result3"

Branching in behavior formulas

In the following examples, a Text input control named FirstName has the value "John" typed into it.

Formula Description Result
If( ! IsBlank( FirstName.Text ), Navigate( Screen1, ScreenTransition.None ) ) The condition is true, and the Navigate function is executed. You can use the IsBlank function to test whether a required form field has been filled in. If the text-input box was blank, this formula would have no effect. true

The display is changed to Screen1.
If( IsBlank( FirstName.Text ), Navigate( Screen1, ScreenTransition.None ), Back() ) Without the . operator, the condition is false, and the Navigate function isn't executed. Because the ElseResult has been provided, the Back function is executed. true

The display goes back to the screen that was previously shown.

Step by step

  1. On an empty screen, add a Text input control, and name it Text1 if it doesn't have that name by default.

  2. In Text1, type 15.

  3. Add a label, and set its Text property to the following formulas:

Formula Description Result
Text1.Text With no condition, the return value is the value of the input-,text control. "15"
If( Value(Text1.Text) < 40, "Order more!", Text1.Text ) The condition is true, and the corresponding value is returned. "Order more!"
If( Value(Text1.Text) < 20, "Order MANY more!", Value(Text1.Text) < 40, "Order more!", Text1.Text ) The first condition is true, and the corresponding value is returned. "Order MANY more!"