IF

The IF function is used to do conditional branching.

This function takes pairs of tests (which must be booleans after evaluation) and values (which can be any data type). It then goes through the tests in the order they were provided and outputs the value associated with whatever test is first TRUE. If the value of every test is FALSE, the IF function outputs NULL by default. The IF function also accepts as optional input a value to return in such cases; if such a value was provided, this the the value the IF function will output should none of the given tests are TRUE

Declaration

IF(test_1, value_if_true_1) -> result
IF(test_1, value_if_true_1, value_if_false) -> result
IF(  
 test_1,   
 value_if_true_1,   
 test_2,   
 value_if_true_2) -> result
IF(  
 test_1,   
 value_if_true_1,   
 test_2,   
 value_if_true_2,   
 value_if_false) -> result
IF(  
 test_1,   
 value_if_true_1,   
 test_2,   
 value_if_true_2,  
 ...   
 test_n,   
 value_if_true_n) -> result
IF(  
 test_1,   
 value_if_true_1,   
 test_2,   
 value_if_true_2,  
 ...  
 test_n,   
 value_if_true_n,  
 value if false) -> result

 

Parameters

test_1 (required, type: boolean)
The first condition to test.

value_if_true_1 (required, type: any)
The value to be returned if test_1 is TRUE.

test_2 (optional, type: boolean)
The second condition to test.
If test_1 is FALSE, test_2 will be evaluated; otherwise, value_if_true_1 will be returned. This parameter is optional, but if it is present, then value_if_true_2 must also be provided; this is what will be returned if test_2 is true.

value_if_true_2 (optional, type: any)
The value to be returned if test_2 is TRUE(but test_1 is FALSE).

test_n (optional, type: boolean)
The nth condition to test.
If all previous test parameters are FALSE, test_n will be evaluated, and, if it is true, value_if_true_n will be returned. The IF function supports as many pairs of tests and values as are needed, but they must be provided in pairs.

value_if_true_n (optional, type: any)
The value to be returned if test_n is TRUE(but all previous tests are FALSE).

value_if_false (optional, type: any)
If no tests are TRUE, the result of the value_if_false parameter is returned. This parameter is opitional, if no value_if_false parameter is provided the result will be NULL.

Return Values

result (type: any)
When one of the test parameters is TRUE, this result will be the corresponding value_if_true parameter. Otherwise, the result will be the value_if_false parameter when it is provided and NULL otherwise.

Examples

The following example checks to see if the boolean TRUE is TRUE. It is, and thus IF outputs the associated value, which is the number 1: 

IF(TRUE, 1) -> 1

The following example checks to see if the boolean FALSE is TRUE. It isn't, and because no other pairs of tests and values were provided, there is nothing else for the IF function to test:

IF(FALSE, 1) -> NULL

The following example checks to see if the boolean FALSE is TRUE. It isn't, and because no other pairs of tests and values were provided, there is nothing else for the IF function to test. By default, the IF function outputs NULL if all tests are FALSE, but because a value was given for value_if_false ("This was false"), the function outputs that instead. Note that the values given for value_if_true_1and value_if_false are different data types – value_if_true_1is a number (1), and value_if_false is a string ("This was false"). The values given as potential output of the IF function can be any data type and do not at all depend on each other:

IF(FALSE, 1, "This was false") -> "This was false"

The following example checks to see if 1 = 1 is TRUE. It is, and thus IF outputs the associated value, which is the string "The first test was true". There were other pairs of tests and values given as input, but they do not effect the outcome; once the first test was established TRUE, none of the others were evaluated. It makes not difference that the later tests were equally true: 

IF(  
 1 = 1,  
 "The first test was true",  
 2 = 2,  
 "The second test was true",  
 3 = 3,   
 "The third test was true") -> "The first test was true"

The last example checks to see if 1 = 2 is TRUE. It isn't, and so the function moves on to check if 2 = 2 is TRUE. It is, and thus IF outputs the associated value, which is the string "The second test was true". There was another pair of tests and values given as input, but they do not effect the outcome; once the second test was established TRUE, the third test was not evaluated.

IF(  
 1 = 2,  
 "The first test was true",  
 2 = 2,  
 "The second test was true",  
 3 = 3,   
 "The third test was true",  
 "No tests were true") -> "The second test was true"

Discussion

In the above examples, for the sake of clarity, all variables were hardcoded. They do not need to be; in fact, in practice they will almost never be entirely hardcoded. There is no need to to use software to check and see if an expression is TRUE after you just typed it.

It is not uncommon to use the IF function in tandem with other Airscript functions, particularly functions that output a boolean, such as ISEMAIL, ISEMPTY, or ISPHONE (or other functions beginning with IS). For instance, the following example checks to see if the string "[email protected]" is a valid email. It is, and thus IF outputs the associated value, which is the string "This is a valid email": 

IF(ISEMAIL(support.airkit.com), "This is a valid email") -> "This is a valid email"