Git Product home page Git Product logo

Comments (2)

github-learning-lab avatar github-learning-lab commented on July 19, 2024

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

from ruby-course.

github-learning-lab avatar github-learning-lab commented on July 19, 2024

🎊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."

from ruby-course.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.