Ruby Loop
Loops in Ruby are used to execute the same block of code multiple times. This chapter will detail all the loop statements supported by Ruby.
## Syntax
while conditionalcode end
or
## Syntax
while conditional[:]code end
When _conditional_ is true, execute _code_.
The `do` or `:` in the syntax can be omitted. However, if you want to write a while statement on a single line, you must separate the conditional expression or code block with `do` or `:`.
## Example
$i = 0$num = 5 while$i<$num do puts("In the loop statement i = #$i")$i +=1 end
[Try it Β»](#)
The output of the above example is:
In the loop statement i = 0In the loop statement i = 1In the loop statement i = 2In the loop statement i = 3In the loop statement i = 4
## Syntax
code while condition or begin code end while conditional
When _conditional_ is true, execute _code_.
If the _while_ modifier follows a _begin_ statement without a _rescue_ or _ensure_ clause, _code_ will be executed once before the _conditional_ is checked.
## Example
$i = 0$num = 5 begin puts("In the loop statement i = #$i")$i +=1 end while$i$num do puts("In the loop statement i = #$i")$i +=1; end
[Try it Β»](#)
The output of the above example is:
In the loop statement i = 0In the loop statement i = 1In the loop statement i = 2In the loop statement i = 3In the loop statement i = 4In the loop statement i = 5
## Syntax
code until conditional or begin code end until conditional
When _conditional_ is false, execute _code_.
If the _until_ modifier follows a _begin_ statement without a _rescue_ or _ensure_ clause, _code_ will be executed once before the _conditional_ is checked.
## Example
$i = 0$num = 5 begin puts("In the loop statement i = #$i")$i +=1; end until$i>$num
[Try it Β»](#)
The output of the above example is:
In the loop statement i = 0In the loop statement i = 1In the loop statement i = 2In the loop statement i = 3In the loop statement i = 4In the loop statement i = 5
## Syntax
for variable[, variable ...]in expressioncode end
First, the expression is evaluated to get an object, then _code_ is executed once for each element in _expression_.
## Example
for i in 0..5 puts"The value of local variable is #{i}"end
[Try it Β»](#)
Here, we have defined the range 0..5. The statement `for i in 0..5` allows the value of `i` to range from 0 to 5 (inclusive).
The output of the above example is:
The value of local variable is 0The value of local variable is 1The value of local variable is 2The value of local variable is 3The value of local variable is 4The value of local variable is 5
The _for...in_ loop is almost completely equivalent to:
(expression).each do |variable[, variable...]| code end
However, the for loop does not create a new scope for local variables.
The `do` in the syntax can be omitted. However, if you want to write a for statement on a single line, you must separate the conditional expression or code block with `do`.
## Example
(0..5).each do |i| puts"The value of local variable is #{i}"end
[Try it Β»](#)
The output of the above example is:
The value of local variable is 0The value of local variable is 1The value of local variable is 2The value of local variable is 3The value of local variable is 4The value of local variable is 5
## Syntax
break
Terminates the innermost loop. If called within a block, it terminates the method associated with the block (the method returns nil).
## Example
for i in 0..5 if i>2 then break end puts"The value of local variable is #{i}"end
[Try it Β»](#)
The output of the above example is:
The value of local variable is 0The value of local variable is 1The value of local variable is 2
## Syntax
next
Jumps to the next iteration of the most internal loop. If called within a block, it terminates the block's execution (the _yield_ expression returns nil).
## Example
for i in 0..5 if i<2 then next end puts"The value of local variable is #{i}"end
[Try it Β»](#)
The output of the above example is:
The value of local variable is 2The value of local variable is 3The value of local variable is 4The value of local variable is 5
## Syntax
redo
Restarts the current iteration of the most internal loop, without checking the loop condition. If called within a block, restarts the _yield_ or _call_.
## Example
for i in 0..5 if i Note: Versions 1.9 and later do not support using `retry` in loops.
## Syntax
retry
If _retry_ appears in a rescue clause of a begin expression, it restarts from the beginning of the begin body.
begin do_something rescue retry end
If retry appears within an iteration, a block, or the body of a for expression, it restarts the iteration call. The iteration's arguments are re-evaluated.
for i in 1..5 retry if some_condition end
## Example
for i in 1..5 retry if i>2 puts"The value of local variable is #{i}"end
This will produce the following result and will enter an infinite loop:
The value of local variable is 1The value of local variable is 2The value of local variable is 1The value of local variable is 2The value of local variable is 1The value of local variable is 2............................
YouTip