Conditionals

Back

Learning Goals

  • Use comparison and logical operators
  • Write if/elsif/else statements for conditional logic

Vocabulary

  • comparison operators
  • conditionals
  • if statements
  • logical operators

Comparison Operators

This will connect to Booleans, which you learned about in the Data Types lesson.

Conditional statements, or conditionals, are Ruby statements that evaluate to true or false. The most common operators used for conditions are comparison operators:

  • == (equal)
  • != (not equal)
  • > (greater than)
  • >= (greater than or equal to)
  • < (less than)
  • <= (less than or equal to)
# Does the Integer 5 have the exact same value as the Integer 5?
5 == 5
# --> true

# Is Integer 5 less than the Integer 3?
5 < 3
# --> false

age = 18
# Is the age greater than 12?
age > 12
# --> true

# reassign age variable to 9
age = 9
# Is the age greater than 12?
age > 12
# --> false

Equality vs. Assignment

The most common mistake people encounter when writing conditional statements is related to the difference between = and ==.

  • = is an assignment. It instructs, "take the value on the right side and store it into whatever is on the left side" – it’s telling, not asking.
  • == is a question. It asks, "is the value on the right the exact same as the value on the left?" – it’s asking, not telling.

Optional: Read more about comparison operators.


Comparisons

Complete the following work in an irb session:

  • Check if 3 is less than or equal to 6.
  • Check if "T" is equal to "t".
  • Check if "P" is equal to " P".
  • Check if 10 is not equal to 10.
  • Declare a variable that store your name in a String.
  • Check if the length of your name is greater than or equal to 10. Not sure how to do that programmatically? Google!


For the remaining practice activities in this assignment, you can use irb, create a Ruby file in your Terminal and edit it in VS Code, or use an online IDE such as replit - it’s your preference!

If Statements

If statements are the most common form of conditionals. The logic of an if statement in Ruby follows the same logic in other programming languages.

if

All of our conditional branches will begin with the keyword if. To communicate to the Ruby program that the if statement should end, the keyword end must be used. Any code between the if and end keywords will run only if the condition is true.

if condition
  # code to execute if condition evaluates to true
end

The following example checks if a store is open, and prints out a statment if the store is indeed open:

is_open = true

if is_open == true
  puts "Let's go!"
end

Try It: if statements

Define an age variable and assign it to an Integer. Then, write code that will print out the String "Welcome to the Website" only if the user is 21 or older.


else

In the previous exercises, when the condition evaluates to false, we don’t see any output. In order to have a default response that runs when the condition is false, we need an else statement. Here’s an example with the is_open variable for the store.

is_open = true

if is_open == true
  puts "Let's go!"
else
  puts "Oops. Looks like that store is closed now."
end

Try It: else statements

Go back to your previous exercise with the age variable. Use an else statement that prints out a different message if the user is not 21 or older.


elsif

Use the keyword elsif to create more branches with additional conditions to check.

if condition1
  # code to execute if above condition1 evaluates to true
elsif condition2
  # code to execute if above condition2 evaluates to true
elsif condition3
  # code to execute if above condition3 evaluates to true
else 
  # default code to execute if none of the other conditions evaluate to true
end

Notice that code inside the else statement will only run when none of the previous conditions evaluate to true.

Try It: elsif statements

Continue building on the previous work you've done with the age variable. Write code that will print out the String "Welcome to the Website" only if the user is 21 or older. If the user is under the age of 2, print out "How are you even on this device?". If the user is any other age, print out "You aren't quite old enough to visit, sorry!".


Notice that each of the previous examples have one end keyword. That is paired with the opening if keyword, and is required. What happens if you forget the end keyword?

Optional: Read the Conditionals section from Ruby in 100 Minutes for a different explanation.


More Practice

  • Declare a variable that stores your name in a String.
  • Write an if/else statement - if your name is "Oscar", print "Hello, Oscar!". If not, print "Nice to meet you, Stranger!".
  • Challenge: Use string interpolation to interpolate any non-Oscar name instead of the word "Stranger".

Logical Operators

There are three logical operators in Ruby; we will learn two today:

  • && or and
  • || or or

&& or and

This logical operator will check two values, and both must be true in order for it to return true. Examples follow:

age = 34
time = 2.05

age < 40 && time < 3.0
# true (both meet requirement)

age < 30 && time < 3.0
# false (age is not less than 30)

age < 40 && time < 2.0
# false (time is not less than 2.0)

age < 30 && time < 2.0
# false (neither meet requirement)

|| or or

This logical operator will check two values, and one or both must be true in order for it to return true. Examples follow:

age = 34
time = 2.05

age < 40 || time < 3.0
# true (both meet requirement)

age < 30 || time < 3.0
# true (time meets requirement)

age < 40 || time < 2.0
# true (age meets requirement)

age < 30 || time < 2.0
# false (neither meet requirement)

Logical Operators

Complete the following work in an irb session:

  • Declare two variables - one that stores your age and another person's age.
  • Check if both ages are under 100.
  • Check if at least one age is under 78.
  • Check if your age is over 18 and the other age is under 60.
  • Check if your age is over 21 or if the other age is over 25.
  • Check if your age is under 20 or if the other age is over 40.

Now that you know about logical operators and if statements, let’s combine that new knowledge to complete a final exercise.

More Practice: Logical Operators with Conditions

  • Declare a variable that stores a password.
  • In order for a password to be valid, it needs to have 8 characters or more and include the symbol #.
  • Use logical operators and conditions to give the user feedback based on their password.
  • If the user enters a password that is both 8 characters or more and includes the symbol #, print the statement, "Got it. Your password is: XXXXXXXX" using interpolation to confirm their password.

Check For Understanding

Follow the directions in the README of this GitHub repository, and submit your fork in the submission form.





Back