12. Conditionals and Logical Operators

Now let's see how we can build up logic with our programs.

Comparison Operators

When we want to compare to values to build up logic, we use the comparison operators below.

Operator Example Meaning
== a==b returns true if a and b are equal
!= a != b returns true if a and b are not equal
> a > b returns true if a is larger than b
>= a >= b returns true if a is larger than or equal to b
< a < b returns true if a is smaller than b
<= a <= b returns true if a is smaller than or equal to b

Logical Operators

We also use logical operators to build up logic as well.

Operator Example Meaning
`&&` `A && B` Returns `true` if both `A` and `B` is `true`
`||` `A || B` Returns `true` if either `A` or `B` is `true`
`!` `!A` Returns the opposite of `A`

Here's a basic rundown of the logical operators:

Expression Result
`false && false` `false`
`true && false` `false`
`false && true` `false`
`true && true` `true`
`false || false` `false`
`false || true` `true`
`true || false` `true`
`true || true` `true`


if

Let's cover the concept of if statements. If statements allow programmers to execute code only if a certain property is true. It's very similar to real life.

Real life examples

In real life, we base our decisions off of many factors and situations. Here are some examples:

  • If it is raining, don't go outside. Otherwise go outside.
  • If I am hungry, I want to eat. Otherwise, I don't want to eat. In programming, we can build logic in similar ways as well.

Let's first navigate into our ruby_fundamentals folder:

cd ruby_fundamentals

Inside of this folder, let's create a new file called is_even.rb. You can do this by right clicking on the folder and creating a new file, or you can run the following command:

touch is_even.rb

Open the new file that we just created.

Let's write a method that outputs "#{number} is even!" if the argument that is passed in is an even number, and "#{number} is odd!" if it isn't an even number.

First, let's create a method called is_even? that takes in a parameter n.

def is_even?(n)

end

Next, let's think about the logic we want to create. What we need to do is find out if the number is even. To do this what can we use?

We can use the % operator. If the number is even, then the remainder of dividing the number by 2 should be 0.

In Ruby, we can check if the condition is true with an if condition:

if (condition)
  # then execute code here
else
  # otherwise execute code here
end

To check if the number is even, we can write code like this:

def is_even?(n)
  if n % 2 == 0
    # then execute code here
  else
    # otherwise execute code here
  end
end

In Ruby, parenthesis are optional and are not necessary. if (n % 2 == 0) and if n % 2 == 0 do the same thing. Other programming languages may require the use of parenthesis.

The next task is to print it out to the console. To do this, we can use puts:

def is_even?(n)
  if n % 2 == 0
    puts "#{n} is an even number!"
  else
    puts "#{n} is an odd number!"
  end
end

Now that we have completed our method, let's run the method by calling it:

def is_even?(n)
  if n % 2 == 0
    puts "#{n} is an even number!"
  else
    puts "#{n} is an odd number!"
  end
end

is_even?(1)
is_even?(2)
is_even?(3)
is_even?(4)
is_even?(5)
is_even?(6)
is_even?(7)
is_even?(8)
is_even?(9)
is_even?(10)

Save the file. Let's run the file:

ruby is_even.rb

Your output should look like this:

image.png

Awesome! But what if we wanted to put in more conditions? For example, what if we wanted to print out "#{n} is divisible by 3!" when the number is divisible by 3?

That's where elsif statements are useful.

if condition
  # execute code here
elsif another_condition
  # execute code here
elsif another_condition_2
  # execute code here
else
  # execute code here
end

With elsif, we can check for another condition. We can also attach as many elsif statements as we want as demonstrated above.

How could we check if the number is divisible by 3?

A number divisible by 3 will have no remainder if it is divided by 3. That means we should check if the remainder of dividing the number by 3 is 0. To do this, we can once again use %.

def is_even?(n)
  if n % 2 == 0
    puts "#{n} is an even number!"
  elsif n % 3 == 0
    puts "#{n} is divisible by 3!"
  else
    puts "#{n} is an odd number!"
  end
end

Now let's run the program again:

ruby is_even.rb

Your output should look like this:

image.png

Overview

Coding Challenge