A.11 Loops
while statements:
while boolean
# ruby code here
end
nil ⇒
while
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 statement
Example:
5
limit = while limit > 0
p limit1
limit = limit - 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.