IF (Condition, ThenValue, OtherCondition..., OtherThenValue..., OtherwiseValue?) IF (Condition; ThenValue; OtherCondition...; OtherThenValue...; OtherwiseValue?)

Condition

Logical Logical

The condition to check.

ThenValue

The value to return if the condition is TRUE.

OtherCondition

Logical Logical (accepts many)

An additional condition to check.

OtherThenValue

An additional value to return if the preceding condition is TRUE.

OtherwiseValue

The value to return if no conditions are TRUE. If omitted, a blank value is used.

Returns

The value associated with the first condition which is TRUE (evaluated from left to right). If no condition is TRUE, the OtherwiseValue parameter is returned. If there is no such parameter, a blank value is returned.

Returns one of the given values based on logical conditions. IF (Value1 > 20, 2, Value2 > 40, 4, 6) IF (Value1 > 20; 2; Value2 > 40; 4; 6) returns 2 if Value1 has a value greater than 20, 4 if Value1 does not have a value greater than 20 and Value2 has a value greater than 40, and 6 otherwise.

Multiple conditions

IF supports multiple conditions. Consider this formula:

IF (Value1 > 20, 4, Value1 < 10, 2, - 1)
IF (Value1 > 20; 4; Value1 < 10; 2; - 1)

The preceding formula returns 4 if Value1 is greater than 20. Otherwise, it returns 2 if Value1 is less than 10. If Value1 is not greater than 20, and it is not less than 10, -1 is returned.

Spreadsheets often require you to use the IFS function to use multiple conditions. This IF function supports multiple conditions, though IFS may also be used.

Logical "and" (conjunction)

Use logical functions and logical operators to express more complex relationships. This formula returns "Success!" only if Value1 is greater than 20 and Value2 is less than 10:

IF ((Value1 > 20) && (Value2 < 10), "Success!")
IF ((Value1 > 20) && (Value2 < 10); "Success!")

The AND function is more familiar to spreadsheet users — the && operator is not commonly supported by spreadsheets — and produces the same result:

IF ( AND (Value1 > 20, Value2 < 10), "Success!")
IF ( AND (Value1 > 20; Value2 < 10); "Success!")

Logical "or" (disjunction)

This formula returns "Success!" if Value1 is greater than 20 or Value2 is less than 10:

IF ((Value1 > 20) || (Value2 < 10), "Success!")
IF ((Value1 > 20) || (Value2 < 10); "Success!")

The OR function is, again, more familiar to spreadsheet users than the || operator (which is not commonly supported by spreadsheets) and performs identically:

IF ( OR (Value1 > 20, Value2 < 10), "Success!")
IF ( OR (Value1 > 20; Value2 < 10); "Success!")

Logical "not" (negation)

To negate a logical value (turning FALSE into TRUE and vice versa), use the ! operator or the NOT function.

These formulas both return 2 only if Value1 is not odd:

IF ( ! ISODD (Value1), 2)
IF ( ! ISODD (Value1); 2)

IF ( NOT ( ISODD (Value1)), 2)
IF ( NOT ( ISODD (Value1)); 2)

Blank values

If a third parameter is not passed to IF, a blank value is returned if the condition does not evaluate to TRUE. Both these formulas return a blank value if Value1 is not 10:

IF (Value1 = 10, 2)
IF (Value1 = 10; 2)

IF (Value1 = 10, 2, BLANK ())
IF (Value1 = 10; 2; BLANK ())

To determine if a value is blank, use the ISBLANK function.

This formula returns "No value" if Value1 has a blank value, and the value of Value1 otherwise:

IF ( ISBLANK (Value1), "No value", Value1)
IF ( ISBLANK (Value1); "No value"; Value1)

The specialized IFBLANK function may be used to write an equivalent, shorter formula:

IFBLANK (Value1, "No value")
IFBLANK (Value1; "No value")

ISDEFINED does the reverse. This formula returns "Filled out!" only if a value is associated with Value1 :

IF ( ISDEFINED (Value1), "Filled out!")
IF ( ISDEFINED (Value1); "Filled out!")

Arrays and IF

When initializing arrays, use IF to determine if an element should be part of the array.

This formula returns the array { 2, 4, 6, 8 }{ 2; 4; 6; 8 } only if Value1 is greater than 20, and returns the array { 2, 4, 8 }{ 2; 4; 8 } otherwise:

{ 2, 4, IF (Value1 > 20, 6), 8 }
{ 2; 4; IF (Value1 > 20; 6); 8 }

If the condition is a logical array, IF is applied to each of its elements, and returns an array. This formula returns the array { 0, 20, 30 }{ 0; 20; 30 }, as only the first element of { 1, 2, 3 }{ 1; 2; 3 } is not greater than 1:

IF ({ 1, 2, 3 } > 1, { 10, 20, 30 }, 0)
IF ({ 1; 2; 3 } > 1; { 10; 20; 30 }; 0)

Spreadsheet users often use this technique to filter values before adding them together. This formula returns 50:

SUM ( IF ({ 1, 2, 3 } > 1, { 10, 20, 30 }, 0))
SUM ( IF ({ 1; 2; 3 } > 1; { 10; 20; 30 }; 0))

Leaving out the third parameter gives a blank value for each element whose condition is FALSE, which functions like MAX ignore. This formula returns 30:

MAX ( IF ({ "W", "E", "W" } = "W", { 10, 20, 30 }))
MAX ( IF ({ "W"; "E"; "W" } = "W"; { 10; 20; 30 }))

The FILTER function expresses the same idea more directly. These formulas return 50 and 30, respectively:

SUM ( FILTER ({ 10, 20, 30 }, { 1, 2, 3 } > 1))
SUM ( FILTER ({ 10; 20; 30 }; { 1; 2; 3 } > 1))

MAX ( FILTER ({ 10, 20, 30 }, { "W", "E", "W" } = "W"))
MAX ( FILTER ({ 10; 20; 30 }; { "W"; "E"; "W" } = "W"))

IF alternative: SWITCH

Consider using SWITCH instead of this function if you need to test for equality against the same value. These formulas are equivalent:

IF (Value1 = 2, 1, Value1 = 4, 2, Value1 = 8, 3)
IF (Value1 = 2; 1; Value1 = 4; 2; Value1 = 8; 3)

SWITCH (Value1, 2, 1, 4, 2, 8, 3)
SWITCH (Value1; 2; 1; 4; 2; 8; 3)

IF alternative: CHOOSE

CHOOSE selects which parameter to return based on the first parameter. If the first parameter is 3, the third parameter is returned. If it is 20, the 20th parameter is returned.

These formulas are equivalent:

IF (Value1 = 1, 10, Value1 = 2, 20, Value1 = 3, 30)
IF (Value1 = 1; 10; Value1 = 2; 20; Value1 = 3; 30)

SWITCH (Value1, 1, 10, 2, 20, 3, 30)
SWITCH (Value1; 1; 10; 2; 20; 3; 30)

CHOOSE (Value1, 10, 20, 30)
CHOOSE (Value1; 10; 20; 30)

Examples

IF (TRUE, 2, 4)
IF (TRUE; 2; 4)

Returns 2.

IF (FALSE, 2, 4)
IF (FALSE; 2; 4)

Returns 4.

IF (Value1 > 20, 2, 4)
IF (Value1 > 20; 2; 4)

Returns 2 only if Value1 has a value greater than 20. Otherwise, 4 is returned.

IF (Value1 > 20, 4, IF (Value1 < 10, 2, - 1))
IF (Value1 > 20; 4; IF (Value1 < 10; 2; - 1))

Returns 4 if Value1 is greater than 20, 2 if the same value is less than 10 and -1 otherwise.

IF (Value1 > 20, 4, Value1 < 10, 2, - 1)
IF (Value1 > 20; 4; Value1 < 10; 2; - 1)

Returns 4 if Value1 is greater than 20, 2 if the same value is less than 10 and -1 otherwise. This formula is equivalent to IF (Value1 > 20, 4, IF (Value1 < 10, 2, - 1)) IF (Value1 > 20; 4; IF (Value1 < 10; 2; - 1)), but does not use nested IF functions, making the formula easier to write and maintain.

IF ((Value1 > 20) && ISODD (Value1), 2, 4)
IF ((Value1 > 20) && ISODD (Value1); 2; 4)

Returns 2 only if Value1 is greater than 20 and said value is odd. Otherwise, 4 is returned. That means that 21 and 23 qualify, but 22 and 11 do not.

IF ( AND (Value1 > 20, ISODD (Value1)), 2, 4)
IF ( AND (Value1 > 20; ISODD (Value1)); 2; 4)

Returns 2 only if Value1 is greater than 20 and said value is odd. Otherwise, 4 is returned. This formula returns the exact same result as IF ((Value1 > 20) && ISODD (Value1), 2, 4) IF ((Value1 > 20) && ISODD (Value1); 2; 4), with the only difference being the use of the AND function, instead of the && operator. AND is a standard spreadsheet function, whereas the && operator is not commonly supported by spreadsheets.

IF ((Value1 > 20) || ISODD (Value1), 2, 4)
IF ((Value1 > 20) || ISODD (Value1); 2; 4)

Returns 2 only if Value1 is greater than 20 or said value is odd. Otherwise, 4 is returned. That means that 21, 22, 23, 1, 3 and 5 qualify, but 2, 4 and 6 do not.

IF ( OR (Value1 > 20, ISODD (Value1)), 2, 4)
IF ( OR (Value1 > 20; ISODD (Value1)); 2; 4)

Returns 2 only if Value1 is greater than 20 or said value is odd. Otherwise, 4 is returned. This formula returns the exact same result as IF ((Value1 > 20) || ISODD (Value1), 2, 4) IF ((Value1 > 20) || ISODD (Value1); 2; 4), with the only difference being the use of the OR function, instead of the || operator. OR is a standard spreadsheet function, whereas the || operator is not commonly supported by spreadsheets.

IF ( ! ISEVEN (Value1) && NOT ( ISEVEN (Value2)), "Y")
IF ( ! ISEVEN (Value1) && NOT ( ISEVEN (Value2)); "Y")

Returns "Y" only if Value1 and Value2 are not even. The ! operator can also be expressed using the standard spreadsheet function NOT.

{ 2, 4, IF (Value1 > 20, 6), 8 }
{ 2; 4; IF (Value1 > 20; 6); 8 }

Returns the array { 2, 4, 6, 8 }{ 2; 4; 6; 8 } only if Value1 is greater than 20. Otherwise, the array { 2, 4, 8 }{ 2; 4; 8 } is returned. In other words, IF inside of an array can be used to determine if an array element should be part of the array by leaving out the third parameter to IF.

{ 2, 4, IF (Value1 > 20, 6, BLANK ()), 8 }
{ 2; 4; IF (Value1 > 20; 6; BLANK ()); 8 }

Returns the array { 2, 4, 6, 8 }{ 2; 4; 6; 8 } only if Value1 is greater than 20. Otherwise, the array { 2, 4, BLANK (), 8 }{ 2; 4; BLANK (); 8 } is returned. This example illustrates that leaving out the third parameter to IF results in the element being omitted entirely from the array, whereas using BLANK () BLANK () as the third parameter instead includes a blank value.

{ 2, 4 } | IF (Value1 > 20, { 6 }, {}) | 8
{ 2; 4 } | IF (Value1 > 20; { 6 }; {}) | 8

Returns the array { 2, 4, 6, 8 }{ 2; 4; 6; 8 } only if Value1 is greater than 20. Otherwise, the array { 2, 4, 8 }{ 2; 4; 8 } is returned. This example uses the | operator, which joins arrays together. It produces the same result as { 2, 4, IF (Value1 > 20, 6), 8 }{ 2; 4; IF (Value1 > 20; 6); 8 }.

fxEngine Shell Want this in your own terminal? A standalone version is coming. Get notified.
Loading fxEngine Shell