A.11 Loops
while statements:
while boolean
# ruby code here
end
⇒ nilwhile is similar to if. The difference is everytime the execution of the program reaches the end it jumps back and evaluates the truthiness of the condtion next to the while statement and decides whether or not to execute the code within the while loop.
while condition
# do something while condition is true
end # jump back to the while statementExample:
limit = 5
while limit > 0
p limit
limit = limit - 1
end 5
4
3
2
1
Note:
If the condition next to the while always evaluates to be “truthy,” then the program will be stuck in a neverending loop, infamously known as an
infinite loop.