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)

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!)

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.

If Statements

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

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 an if. To communicate to the Ruby program that the if statement should end, they keyword end must be used. Any code following the if, but before the end will run 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 to send directions if the store is indeed open:

is_open = true

if is_open == true
  puts "send directions
end

if statements

Write code that defines 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.


elsif

Use an elsif to create more branches.

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
end

elsif statements

Write code that defines 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. If the user is under the age of 2, print out "How are you even on this device?".


else

Code inside an else will run when none of the previous conditions are true.

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
  # code to execute if all previous conditions evaluate to false
end

else statements

You will now build 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", puts "Hello, Oscar!". If not, puts "Hello, Stranger!".
  • Challenge: Use string interpolation to interpolate any non-Oscar name in after the word "Stranger".

Check For Understanding

Follow the directions in the README of this GitHub repository, and submit your fork in the submission form. Upload the video (requirements outlined in the GitHub repo) to your Slack small group channel.



Back