Guide to Compound Conditionals in Ruby
We have seen `if-else` and `unless` statements in the previous lesson, but sometimes, you may want to combine multiple conditionals together to perform a certain task. In this lesson, I'm going to show you how to do it with compound, also known as nested conditionals.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So far in this section we have seen if, unless, if/else, and if/elsif statements and how they work in Ruby programs. Additionally there are times when you may want to combine multiple conditionals together to enable a more granular data check. In this guide I'm going to show you how to utilize multiple conditionals per line.

Compound Conditionals Code Example

Let's say you want to check for two nested conditions. Technically you could accomplish the desired output with this code:

x = 10
y = 100
z = 10

if x == y
  if x == z
    puts "equal to everything"
  end
end

This code checks if x and y are the same, and if true, also checks if x and z are the same. If the nested condition is also satisfied, then it prints "equal to everything".
Even though this works it's pretty ugly and could lead to code that's hard to read and debug. Thankfully there is another way to create the same behavior:

if x == y && x == z
  puts "from the if statement"
end

If you run this program you'll see nothing gets printed out. This is because both the statements are not true, but only one is true. That is: x is not equal to y, but x is equal to z. Since the && means that both statements need to be true, the message was not printed.

Now, if I change && to ||, then we are asking Ruby to print the message if only one statement is true. In other words, || stand for or while && stands for and.

if x == y || x == z
  puts "from the if statement"
end

Since only one condition has to be true this version of the program will print out the text.

large

Technically, you can replace the symbols with the words or and and, but there are some small differences that can throw errors, so I prefer to use the symbols. So, this is how you can implement compound conditionals in Ruby.

Compounded Compound Conditionals

Ruby doesn't limit you to two conditionals, technically you can place as many conditions as you want.

if (x == 10 && x == z) || x == y
  puts "from the if statement"
end

This code will print the statement for you. This is because Ruby follows the order of operations (the same order of operations we learned in the Number section of the course), so it will check the code within the parentheses first. If this value is true it sees the || statement, and since it already knows that the left side is true it runs the code inside of the if statement.

If you change || to &&, then nothing gets printed because the second part of the conditional is not true.

Additional Conditional Operators

In this guide I primarily used the == operator, but you can also use >, <, >=, <= and != operators in conditionals. These represent the following statements:

  • > - greater than
  • < - less than
  • >= - greater than or equal to
  • <= - less than or equal to
  • != - not equal to

As a final note, remember that parentheses determines the order of operations, so you can use them if you want one part of the condition to be checked first. This can be particularly useful in a real-world application where the values that need to be checked are dynamic. I recommend that you play around with the parentheses and conditionals from the examples in this guide so that you can become completely familiar with how they can control data flow for programs.