...

/

What are Conditionals?

What are Conditionals?

This lesson gives an insight into conditionals in Ruby and their usage.

If this is true, then do that. Otherwise, do something else.

Often we want to check for a certain condition, and then based on it, do either one thing or another. Or we want to do something only if a condition is true.

All practical programming languages have some way of expressing this, and in Ruby it looks like so:

Press + to interact
number = 5
if number.between?(1, 10)
puts "The number is between 1 and 10"
elsif number.between?(11, 20)
puts "The number is between 11 and 20"
else
puts "The number is bigger than 20"
end

You can probably guess what it does, and how it works, can’t you?

Explanation

Let’s walk through it one by one:

  • If you run this code it will print out The number is between 1 and 10 ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy