When each method or while statement is used for repeated processing, there is no end and the processing is repeated forever. Of course, it puts a load on the personal computer.
number = 0
while number >= 0
puts number
number += 1
end
Continue to add 1 to number and output.
while conditional expression
#Processing to repeat when the conditional expression is true
end
If the conditional expression is not true like the if statement, the process will not be executed. If you put true in the conditional expression, it will surely be repeated.
Use break. For example
number = 0
while number >= 0
if number == 100
break
end
puts number
number += 1
end
Use the if statement to end the process when the number reaches 100. That is, the final output is 99.
-[x] The while statement processes only when the conditional expression is true. -[x] Infinite routes can be stopped with break.
I don't think there are many situations where an infinite loop occurs.
Recommended Posts