If
The If function returns a value, between two given values, based on a condition.
Definition
If(condicion, v1, v2)Parameters
| Name | Description | Data type |
|---|---|---|
| condicion | Logical condition to be evaluated. | boolean |
| v1 | Value to return if the condition is true. | any |
| v2 | Value to return if the condition is false. | any |
Examples
Conditional division example
The following example uses the If function to check whether variable x has a value of zero, in which case it reports an error. Otherwise, it returns the result of dividing 150 by the value of x:
If(x = 0, Error('El valor no puede ser cero'), 150 / x)For a value of x equal to zero, an error will be obtained. For any other value, the result of dividing 150 by the value of x will be returned.
Example to get the maximum of two numbers
The following example uses the If function to return the maximum value between two variables x1 and x2. Note that for this particular case, it would be simpler to use the Max function.
If(x1 > x2, x1, x2)This example will always return the maximum between the two values passed in x1 and x2.