IF, ELSE IF and ELSE
Jul 23, 2017 · 1 min read
Writing code can be very tedious. I ran into a problem when I was trying to write a code that would display one thing if a statement was true and another if the statement was false. This is when I ran into the elseif command. If the expression is false, the inner block of an if-statement is not reached. The statements are not executed. For example, this is what it looks like in ruby:
# Two integers.
a = 1
b = 2
# Use if, elsif, else on the integers.
if a > b
# Not reached.
print "X"
elsif a == b
# Not reached.
print "Y"
else
# This is printed.
print "Z"
end
The if statement is how we create a branch in our program. The if statement includes a true-or-false expression. If that expression is equal to true, then ruby will execute the puts/prints statement that follows the if statement. However, if the expression is false, then ruby skips the puts/prints.
