By setting true in the conditional expression of the while statement, it is possible to "loop the process unless the process is stopped".
Simple example
while true #By setting the conditional expression to true, the processing can be continued as long as the processing is not stopped.
puts "Please enter the number 1 or 2"
number = gets.to_i #The character string entered by the gets method is to_Convert to a numerical value with the i method and assign it to the variable number.
puts "Entered number" #If a string is entered instead of a number, 0 is displayed.
puts number
if number == 1
puts "The number you entered is#{number}is." #After execution, return to the second line (loop)
elsif number == 2
puts "The number you entered is#{number}is." #After execution, return to the second line (loop)
else
puts "The number you entered is other than 1 or 2. I'm done."
break #If you enter a number or character string other than 1 or 2, the else statement is executed and the loop ends.(break).
end
end
As long as it remains true, the process will continue, so you have to stop it somewhere. You can force it to stop by typing Ctrl + C on the console, but you'll need to set break to do that in your code.
Recommended Posts