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 variablenumberon the first line is5, and for this number the method callnumber.between?(1, 10)returnstrue. Ruby will therefore execute the code in theifbranch: Theifbranch is the block of code that comes after the line with theifstatement, and that is indented by two spaces. Once it is done executing theifbranch Ruby will simply ignore the rest of the statement.If you change the number
5on 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 theifbranch, and check the next condition on theelsifline:number.between?(11, 20). Now, this method call returns true, because5is between11and20. Ruby will therefore execute theelsifbranch, and print out this message. Again, once it is done executing theelsifbranch Ruby will ignore the rest of the statement.If you now change the number
15to25, 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 theelsebranch, 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
ifstatement and branch. - There can be many
elsifstatements and branches. - There can be one
elsestatement and branch.