ifelse function
A value chosen by a test.
How to write it
ifelse(test, then, else)
The first value when the test is true, the second when it is false.
- test
- A condition that is true or false, such as x > 0. Takes a condition, true or false.
- then
- The result when the test is true. Takes an expression.
- else
- The result when the test is false. Takes an expression.
ifelse(test, then, else, unknown)
With a result for a test that cannot be decided.
- test
- A condition that is true or false, such as x > 0. Takes a condition, true or false.
- then
- The result when the test is true. Takes an expression.
- else
- The result when the test is false. Takes an expression.
- unknown
- The result when the test cannot be decided, such as x > 0 for a symbol x. Takes an expression.
Examples
ifelse(1 < 2, 10, 20)In the solver,
Open in the solverifelse(1 < 2, 10, 20)gives10.ifelse(x > 0, 1, 2, 3)In the solver,
Open in the solverifelse(x > 0, 1, 2, 3)gives3.
Related functions
- increment
Add 1 to a variable in place and return its old value.
- function
A function with no name; also written x => body.
- indicator
A condition as a number: 1 when it is true and 0 when it is false.
- evaluate
Evaluate an expression even where it would be held.
- piecewise
Cases written value if condition, with an optional otherwise case.
- divide_by
Divide a variable by a value in place and return the new value.