[Ruby] [ruby] Drill output

1 minute read






① How to write by entering a numerical value from the terminal ② How to write a conditional branch

First, from ①. If you want to input from the terminal, use gets. If it is gets, the input value will be a character string, so add .to_i after gets to get.to_i. The to_i method is a method for converting to numbers.

② (conditional branch) uses an if statement. This time, we also use elsif and else to separate the conditions with three patterns. The condition this time is a comparison of numerical values. Comparison operators such as <and == are used for comparison. Since this time the comparison is less than 10 or less than 0, use <=. When = is attached, it means the following or more. The difficult thing about this conditional expression is which condition to write from. image.png In this way, if the conditional expression that is a number of 10 or less is entered first, even if input = -1, it will not be output if it is a number of 0 or less because it applies to input <= 10. Therefore, if it is not less than 10, it is greater than 10. Therefore, the condition when the value is greater than 10 is written after else.