On this page:
2.11.1 Logical Expressions
2.11.2 Logical Values

2.11 Conditional Expressions

There are many operations in which the result is dependent on a specific test. For example, the mathematical function \(|x|\), which computes the absolute value of \(x\), is equivalent to the symmetric of \(x\) if \(x\) is negative, and to \(x\) itself otherwise. Using the mathematical notation we have:

\[|x|= \begin{cases} -x, & \text{if $x<0$}\\ x, & \text{otherwise.} \end{cases}\]

This function needs to test if the argument is negative in order to choose one of two alternatives: it either evaluates for the number itself or for its symmetrical value.

Expressions whose result depends on one or more tests are called conditional expressions.

2.11.1 Logical Expressions

A conditional expression follows the structure “if expression then ..., otherwise ...”. The expression that determines whether to use the branch “if” or the branch “otherwise”, is called a logical expression and is characterized for having its value interpreted as either true or false. For example, the logical expression x < 0 tests if the value of x is less than zero; if it is, the expression’s evaluation will return true, otherwise it will return false.

2.11.2 Logical Values

Julia considers true and false as the only members of a special data type called logical or boolean data.

Boolean algebra was named after George Boole, the English mathematician that invented the algebra of logic.

In Julia, conditional expressions require the condition used to decide which expression to evaluate to be of boolean type.