Search⌘ K
AI Features

Combining Conditions

Explore how to combine multiple conditions in Ruby using AND (&&) and OR (||) operators within if statements. Understand when and how Ruby evaluates these conditions and how to apply them to real-world scenarios, helping you write clearer and more efficient conditional logic.

We'll cover the following...

and and OR

We can combine conditions that go right after the if statement. Sometimes, we need to perform multiple checks:

if ...
  puts ...
end

There are two ways of combining the conditions: AND and OR. Each way can be represented by the characters && (double ampersand) and || (double pipe).

Example

Ruby
# Combining Conditions
puts 1 == 1 && 2 == 2 # it will display true because both conditions are true
puts 1 == 5 && 2 == 2 # it will display false because 1st condition is false
puts 1 == 5 || 2 == 2 # it will display true because OR opearator is used and one condition is true

Explanation

It is important to note that we can also ...