Skip to content

Esta página también está en español.

Léela en español

このページは日本語でも読めます。

日本語で読む

本页也有简体中文版本。

阅读简体中文版本

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)=10\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
  • ifelse⁡(x>0,1,2,3)=3\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
  • 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.