Logical operators are used to form more complex criteria that test more than one condition for an if expression.
Ruby has three logical operators: and (&&), or (||), and not (!).
A conditional using and evaluates as true if, and only if, both of its operands are true. Otherwise, the entire conditional evaluates to false.
a = 42
b = 8
if a > 7 && b < 11
# outputs “Yes”
The or (||) operator evaluates to true if either (or both) of its operands are true, and false if both operands are false.
a = 3
b = 5
if a ==3 || b > 10
# outputs “Welcome”
The not (!) operator reverses the state of a single operand.
The result of not true is false, and not false is true.
a = 7
# outputs false
In this code, a>5 evaluates to true and then the not operator reverses it to false.
CLICK ON THE BUTTON BELOW TO GO TO THE RUBY MAIN COURSE PAGE.
© License: All Rights Reserved
CONTACT HOLLA TECH – LEARN SUPPORT