Editing
Logical Operators in Elixir
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Logical Operators in Elixir== 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=== 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 (&&)==== 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 (||)==== 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=== 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=== 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=== 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== 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== * [[if-else statement in Elixir]] * [[Comparison Operators in Elixir]] * [[Control flow in Elixir]] [[Category:Elixir]]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information