jmsevillam / ruby-course

An introduction to Ruby

Home Page:https://lab.github.com/everydeveloper/introduction-to-ruby

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using If/Else Statements

github-learning-lab opened this issue · comments

Using If Statements for Conditional Execution

Being able to output strings and collect inputs is certainly useful, though being able to check conditions and execute specific code according to those conditions is where the real power of development begins. If Statements allow us the ability to check if something is true, and if that something is true, then a block of code will run.

To get us started, let's open up the file named montys-color.rb in our text editor and we'll create a program similar to our last project. We're going to prompt the user for their favorite color, assign that input to a variable, and then output a string containing the result of that input.

In lesson three, uncomment lines three, four, and five and then you should have the following:

puts "What is your favorite color?"
color = gets.chop
puts "Wow! My favorite color is #{color} also!"

If you save the file and run it in the terminal, you'll see that it works like our first project.

But what if your favorite color isn't the same as the program's? What if we want our program to only output that response if the user inputs blue as their favorite color? That's where If statements come into play.

We're going to modify our code to create an If statement that does just that, but first, commit and push the changes you've made to the repo.

With the project backed up, let's create put that output into an If statement. Let's comment out line five by putting the # back onto the front of the code and uncomment lines nine, ten, and eleven in lesson four. You should have the following code:

if color == "blue"
    puts "Wow! My favorite color is #{color} also!"
end

What's nice about Ruby is that it prioritizes readability because it's written like plain English. This code is saying, "If color is equal to blue, then put "Wow! My favorite color is blue also!"

If statements evaluate for true or false. If the expression that follows If is true, then it means the condition is met and the code between If and end runs. If the expression is false, the code does not run.

We use == to check if the two values are equal. Since a single equal sign is used to set a value, we use two equal signs to compare values.

End indicates the end of the If statement.

If we run this code in the terminal, and enter "blue" in the prompt, we will see our string printed on the screen. If we enter anything else, our string will not print. But what if we want something different to print even when the input isn't "blue?"

Commit and push the changes you've made to find out how.

git add .
git commit -m"add if statement"
git push

Using Else Statements

Often times we want control over what happens if certain conditions aren't met. In Ruby, we can do that by using Else statements. Else statements allow us to run a different block of code when the If statements condition returns false.

Say we want our program to return, "I'm not a big fan of {color}" when the user inputs anything other than blue as their favorite color. To accomplish this, comment out the code on lines nine, ten, and eleven and uncomment the code on lines 15-19 in lesson five. You should have the following code:

if color == "blue"
    puts "Wow! My favorite color is #{color} also!"
else
    puts "I'm not a big fan of #{color}."
end

Now when we save and run our program, if we enter yellow rather than blue, we'll see that our program isn't the biggest fan of yellow.

What's great about Else statements is it gives us developers control over what happens when conditions aren't met. Sometimes, we don't want anything to happen, and in those cases sticking with only an If statement will be all you need, but it's always nice to have a bit more agency in directing your code how you want.

If/Else statements are powerful tools and knowing how to use them will prove fun and useful as you continue your journey of programming.

Commit and push these final changes to the repo.

git add .
git commit -m"add else statement"
git push

🎊Congratulations! You've written and ran a Ruby program that not only prints strings, but takes user input and prints one of two separate responses depending on that input. Great job and if you are ready to learn more, you can check out the Learning Lab for more courses.

Now that you've completed this tutorial, there are plenty more concepts to dive into.

Here are some suggestions for things you can do to expand on what we learned here today:

  • Look into Elsif Statements to create a third branching pathway. For example, if the user inputs "red," make the program respond with something like "It's not as good as blue, but red isn't too bad."
  • Look into the downcase method and implement it into the project so that no matter what case the user inputs "blue" in, they'll see the expected response.
  • Look into Logical Operators to make the program respond with their favorite color being either "blue" or "green."