Introduction
Comparison operators help you check if things are equal, bigger, or smaller. They let your program make decisions.
Comparison operators help you check if things are equal, bigger, or smaller. They let your program make decisions.
a == b # equals a != b # not equals a > b # greater than a < b # less than a >= b # greater than or equal to a <= b # less than or equal to
Use double equals (==) to check if two things are the same.
Use != to check if two things are different.
5 == 5 # true 5 == 3 # false
"cat" != "dog" # true "cat" != "cat" # false
10 > 7 # true 3 < 1 # false
4 >= 4 # true 2 <= 1 # false
This program prints true or false for different comparisons.
puts 10 == 10 puts 5 != 3 puts 7 > 9 puts 4 <= 4
Remember, == checks value equality, not object identity.
Comparison operators return true or false.
You can use them in if statements to control your program.
Comparison operators compare two values.
They return true or false.
Use them to make decisions in your code.
== checks equality, = assigns values, != checks inequality, and > checks if left is greater than right.==.== -> Option Aa is not equal to variable b in Ruby?!= to check if two values are not equal.a != b is correct. a =! b is invalid syntax. a <> b is not Ruby syntax. a !== b is JavaScript syntax, not Ruby.a != b -> Option Da = 5
b = 10
puts a < b
a (5) is less than b (10).a < b is true.if x = 10
puts "x is 10"
end
= which assigns 10 to x, not compares.x equals 10, use == instead of =.n is between 10 and 20 inclusive in Ruby. Which expression correctly uses comparison operators to do this?n to be at least 10 and at most 20, so both conditions must be true.n >= 10 && n <= 20 uses n >= 10 && n <= 20, which correctly checks both limits.10 <= n <= 20 uses chained comparison which is invalid in Ruby.n > 10 || n < 20 uses OR (||), which allows values outside the range.n == 10 && n == 20 checks if n equals both 10 and 20 simultaneously, which is impossible.Pass the test and this pattern moves unknown