Conditionals
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:
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?
Let’s walk through it one by one:
If you run this code it will print out
The number is between 1 and 10
, because the number assigned to the variablenumber
on the first line is5
, and for this number the method callnumber.between?(1, 10)
returnstrue
. Ruby will therefore execute the code in theif
branch: Theif
branch is the block of code that comes after the line with theif
statement, and that is indented by two spaces. Once it is done executing theif
branch Ruby will simply ignore the rest of the statement.If you change the number
5
on the first line to15
, and run the code again, then it will print outThe number is between 11 and 20
. In this case Ruby will, again, first check the first conditionnumber.between?(1, 10)
, but this time this method call returnsfalse
. Therefore, Ruby will ignore theif
branch, and check the next condition on theelsif
line:number.between?(11, 20)
. Now, this method call returns true, because5
is between11
and20
. Ruby will therefore execute theelsif
branch, and print out this message. Again, once it is done executing theelsif
branch Ruby will ignore the rest of the statement.If you now change the number
15
to25
, and run the code again, then it will print outThe number is bigger than 20
. Again, Ruby will first check the first condition, and find it returnsfalse
. It will check the second condition, which now also returnsfalse
. Therefore Ruby will then execute theelse
branch, and print out that message.
The elsif
and else
statements and branches are optional: you don’t need to
have them. You can have an if
statement without elsif
or else
branches,
an if
statement only with an else
, or you could have an if
statement with
one or more elsif
statements. Or combine all of them together:
- There must be an
if
statement and branch. - There can be many
elsif
statements and branches. - There can be one
else
statement and branch.