Logical Operators Part 4
Part of the Fundamentals section of Coddy's Ruby journey — lesson 20 of 88.
When you combine multiple logical operators in a single expression, Ruby evaluates them in a specific order based on operator precedence. Understanding this order helps you write correct conditions and avoid unexpected results.
The precedence from highest to lowest is: ! (not), then && (and), then || (or). This means ! is evaluated first, followed by &&, and finally ||:
true || false && false # true (&& evaluates first)
# Same as: true || (false && false)
!false && true # true (! evaluates first)
# Same as: (!false) && trueYou can use parentheses to override the default precedence and make your intentions clear:
(true || false) && false # false (parentheses force || first)
true || (false && false) # true (default behavior)Even when parentheses aren't strictly necessary, adding them often makes complex expressions easier to read. When in doubt, use parentheses to explicitly show how conditions should be grouped.
Challenge
EasyYou are provided with the following variables:
x = true
y = false
z = true
a = falseEvaluate the following expressions and display each result on a separate line. Pay attention to operator precedence: ! is evaluated first, then &&, and finally ||.
x || y && z!y && zx && y || z!a || y && zx && !y && z(x || y) && ax || (y && a)!(x && y) || z
Each output should be either true or false.
Cheat sheet
Ruby evaluates logical operators in a specific order based on operator precedence:
!(not) - highest precedence&&(and) - medium precedence||(or) - lowest precedence
Examples of operator precedence:
true || false && false # true (&& evaluates first)
# Same as: true || (false && false)
!false && true # true (! evaluates first)
# Same as: (!false) && trueUse parentheses to override default precedence or improve readability:
(true || false) && false # false (parentheses force || first)
true || (false && false) # true (default behavior)Try it yourself
# Variables are already defined for you
x = true
y = false
z = true
a = false
# TODO: Evaluate each expression and print the result on a separate line
# Remember operator precedence: ! first, then &&, then ||
# 1. x || y && z
puts # your expression here
# 2. !y && z
puts # your expression here
# 3. x && y || z
puts # your expression here
# 4. !a || y && z
puts # your expression here
# 5. x && !y && z
puts # your expression here
# 6. (x || y) && a
puts # your expression here
# 7. x || (y && a)
puts # your expression here
# 8. !(x && y) || z
puts # your expression hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False