Logical Operators in Elixir

From Elixir Wiki
Jump to navigation Jump to search

Logical Operators in Elixir[edit]

Elixir provides various logical operators that are used to evaluate conditions and perform logical operations. These operators allow you to combine multiple conditions and control the flow of your program based on these conditions. In this article, we will explore the logical operators available in Elixir and how they can be used in your programs.

Boolean Operations[edit]

Elixir provides two main boolean operators, namely `and` and `or`, which are used to perform logical AND and OR operations, respectively. Here's how these operators can be used:

AND Operator (&&)[edit]

The `&&` operator returns `true` if both the left and right operands are evaluated to true. Otherwise, it returns `false`. Syntax: `left_expr && right_expr`

Example: ```elixir true && true #=> true true && false #=> false ```

OR Operator (||)[edit]

The `||` operator returns `true` if either the left or right operands are evaluated to true. It returns `false` only if both operands are false. Syntax: `left_expr || right_expr`

Example: ```elixir true || false #=> true false || false #=> false ```

Negation Operator[edit]

Elixir also provides a negation operator (`not`) that inverses the logical value of an expression. It returns `true` if the expression is false and `false` if the expression is true. Syntax: `not expression`

Example: ```elixir not true #=> false not false #=> true ```

Short-Circuit Evaluation[edit]

Elixir supports short-circuit evaluation for logical operators, which means that the right operand is only evaluated if the left operand does not determine the result. This can be useful when working with conditional statements. Here's an example:

```elixir defmodule ExampleModule do

 def function_with_short_circuit_evaluation(true) do
   IO.puts("Hello, World!")
 end
 def function_with_short_circuit_evaluation(false) do
   IO.puts("Condition is false.")
 end

end

ExampleModule.function_with_short_circuit_evaluation(false || true) ```

In the above example, since the left operand `false` evaluates to false, the right operand `true` is not evaluated, and the output will be "Condition is false."

Operator Precedence[edit]

When using multiple logical operators in a single expression, it is important to consider the operator precedence. In Elixir, the `not` operator has the highest precedence, followed by `and`, and then `or`. Parentheses can be used to override the default precedence. Here's an example:

```elixir true || false && not true #=> true ```

In this example, `not true` is evaluated first due to its higher precedence, followed by `false && not true`, and finally `true || (false && not true)`.

Conclusion[edit]

Logical operators are fundamental tools in programming for evaluating conditions and controlling the flow of your programs. Elixir provides the `and`, `or`, and `not` operators to handle logical operations. By understanding and effectively using these operators, you can create more expressive and powerful Elixir programs.

See Also[edit]