# piecewise function

Cases written value if condition, with an optional otherwise case.

## How to write it

### piecewise(case, ...)

The first case whose condition holds; end with value otherwise.

**case**

A value and the condition it holds under, written value if condition. Takes a value with its condition.

## Examples

- $\displaystyle \operatorname{subs}\left(\begin{cases} x & \text{if } x > 0 \\ -x & \text{otherwise} \end{cases}, x \to -3\right) = 3$

  `subs(piecewise(x if x > 0, -x otherwise), x -> -3)`

  In the solver, `subs(piecewise(x if x > 0, -x otherwise), x -> -3)` gives `3`.

  [Open in the solver](https://hilberon.com/?q=subs%28piecewise%28x%20if%20x%20%3E%200%2C%20-x%20otherwise%29%2C%20x%20-%3E%20-3%29)

## Functions its examples use

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

  Substitute a rule, or a list of rules, in an expression.

## Related functions

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

  Subtract 1 from a variable in place and return the new value.
- [indicator](https://hilberon.com/library/indicator)

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

  Add 1 to a variable in place and return the new value.
- [increment](https://hilberon.com/library/increment)

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

  Evaluate an expression that was held.
- [ifelse](https://hilberon.com/library/ifelse)

  A value chosen by a test.

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