# 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

- $\displaystyle \operatorname{ifelse}(1 < 2, 10, 20) = 10$

  `ifelse(1 < 2, 10, 20)`

  In the solver, `ifelse(1 < 2, 10, 20)` gives `10`.

  [Open in the solver](https://hilberon.com/?q=ifelse%281%20%3C%202%2C%2010%2C%2020%29)
- $\displaystyle \operatorname{ifelse}(x > 0, 1, 2, 3) = 3$

  `ifelse(x > 0, 1, 2, 3)`

  In the solver, `ifelse(x > 0, 1, 2, 3)` gives `3`.

  [Open in the solver](https://hilberon.com/?q=ifelse%28x%20%3E%200%2C%201%2C%202%2C%203%29)

## Related functions

- [increment](https://hilberon.com/library/increment)

  Add 1 to a variable in place and return its old value.
- [function](https://hilberon.com/library/function)

  A function with no name; also written x => body.
- [indicator](https://hilberon.com/library/indicator)

  A condition as a number: 1 when it is true and 0 when it is false.
- [evaluate](https://hilberon.com/library/evaluate)

  Evaluate an expression even where it would be held.
- [piecewise](https://hilberon.com/library/piecewise)

  Cases written value if condition, with an optional otherwise case.
- [divide_by](https://hilberon.com/library/divide_by)

  Divide a variable by a value in place and return the new value.

---
Source: https://hilberon.com/library/ifelse
