A.10 Conditionals
Basic Anatomy of multibranch if
statements:
if condition1
# do something if condition1 is true
elsif condition2
# do something if condition2 is true
else # if both condition1 and condition2 were falsy
# do something else
end
Example:
"Type a number less than 10 and greater than 0:"
p # gets user input, removes newline character, converts the string to integer.
user_input = gets.chomp.to_i if user_input == 5
"You win!" # Will print this if the user input is 5
p elsif user_input < 10 && user_input > 0 # check if the user input is valid
"You lose!" # Will print this if the user input is between 1 and 9
p else
"You didn't type in a valid number." # Will print this if the user input is not between 1 and 9
p end
Don’t forget the end
keyword.