if-else
is a conditional statement that runs a set of instructions depending on whether the condition is true or false.
In the code below, if a number is less than ten the line “Try increasing your guess number” is printed; if it’s greater than ten, the line (“Try decreasing your guess number”) is printed.
# number variable.number = 10# if-else statementsif number < 10puts "Try increasing your guess number"elsif number == 10puts "You got this one!"elseputs "Try decreasing your guess number"end
The figure below illustrates how the code above works:
Here’s another example:
# number variable.X = 12# if-else statementsif X > 10 && X < 15puts "The number X must be greater than 10 and less than 15"elseputs "The number X doesn't fall in the range"end