① How to write by entering numerical values from the terminal ② How to write conditional branch
First of all, from ①. If you want to input from the terminal, use gets. If it is gets, the entered value will be a character string, so add .to_i after gets to make it gets.to_i. The to_i method is a method that converts to a number.
(2) (Conditional branch) uses an if statement. This time, we will use elsif and else to divide the conditions into three patterns. The condition this time is a comparison of numerical values. Use comparison operators such as <and == for comparison. This time, the comparison is 10 or less or 0 or less, so use <=. When = is added, it means the following or more. The difficult thing about this conditional expression is which condition to write from. In this way, if you enter the conditional expression with a number of 10 or less first, even if input = -1, it will be applied to input <= 10, so if it is a number of 0 or less, it will not be output. Therefore, if it is not less than 10, it is greater than 10, so the condition for a value greater than 10 is written after else.
Recommended Posts