Usage of loop statements in Ruby
This article describes how to use loop statements in Ruby. Logical loop statements are the basis of each programming language. For more information, see
The loop in Ruby is used to execute the same code block for a specified number of times. This chapter describes the loop statements supported by Ruby in detail.
Ruby while statement:
Syntax:
While conditional [do]
Code
End
Run the code when the condition is true. The condition for a while loop is that reserved words in the Code are separated by line breaks, backslash () or a semicolon.
Instance:
?
1 2 3 4 5 6 7 8 9 |
#! /Usr/bin/ruby $ I = 0 $ Num = 5 While $ I <$ num do Puts ("Inside the loop I = # $ I ") $ I + = 1 End |
This produces the following results:
?
1 2 3 4 5 |
Inside the loop I = 0 Inside the loop I = 1 Inside the loop I = 2 Inside the loop I = 3 Inside the loop I = 4 |
Ruby while rhetoric:
Syntax:
Code while condition
OR
Begin
Code
End while conditional
Run the code when the condition is true.
If the while modifier follows a begin statement but does not have a rescue or ensure clause, the code is evaluated based on the previous condition.
Instance:
?
1 2 3 4 5 6 7 8 |
#! /Usr/bin/ruby $ I = 0 $ Num = 5 Begin Puts ("Inside the loop I = # $ I ") $ I + = 1 End while $ I <$ num |
This produces the following results:
?
1 2 3 4 5 |
Inside the loop I = 0 Inside the loop I = 1 Inside the loop I = 2 Inside the loop I = 3 Inside the loop I = 4 |
Ruby until statement:
Until conditional [do]
Code
End
Run the Code if the condition is false. Reserved words separated by the until Condition Statement from the code, line break or semicolon.
Statement:
?
1 2 3 4 5 6 7 8 9 |
#! /Usr/bin/ruby $ I = 0 $ Num = 5 Until $ I> $ num do Puts ("Inside the loop I = # $ I ") $ I + = 1; End |
This produces the following results:
?
1 2 3 4 5 6 |
Inside the loop I = 0 Inside the loop I = 1 Inside the loop I = 2 Inside the loop I = 3 Inside the loop I = 4 Inside the loop I = 5 |
Ruby until rhetoric:
Syntax:
Code until conditional
OR
Begin
Code
End until conditional
Run the Code if the condition is false.
If the until talker follows the begin statement but does not have a rescue or ensure clause, the code is executed before the conditional evaluation.
Example:
?
1 2 3 4 5 6 7 8 |
#! /Usr/bin/ruby $ I = 0 $ Num = 5 Begin Puts ("Inside the loop I = # $ I ") $ I + = 1; End until $ I> $ num |
This produces the following results:
?
1 2 3 4 5 6 |
Inside the loop I = 0 Inside the loop I = 1 Inside the loop I = 2 Inside the loop I = 3 Inside the loop I = 4 Inside the loop I = 5 |
Ruby for statement:
Syntax:
For variable [, variable...] in expression [do]
Code
End
Each element of code execution is in the in expression.
Instance:
?
1 2 3 4 5 |
#! /Usr/bin/ruby For I in 0 .. 5 Puts "Value of local variable is # {I }" End |
The value range is 0 .. 5. Because the range of values allowed in the statement for I in 0 .. 5 is from 0 to 5 (including 5), the following results are generated:
?
1 2 3 4 5 6 |
Value of local variable is 0 Value of local variable is 1 Value of local variable is 2 Value of local variable is 3 Value of local variable is 4 Value of local variable is 5 |
The for... in loop is almost equivalent:
?
1 |
(Expression). each do | variable [, variable...] | code end |
Except for a for loop, the range of a new local variable is not created. A circular expression is separated from the code, reserved words, a line break, or a semicolon.
Example:
?
1 2 3 4 5 |
#! /Usr/bin/ruby (0 .. 5). each do | I | Puts "Value of local variable is # {I }" End |
This produces the following results:
?
1 2 3 4 5 6 |
Value of local variable is 0 Value of local variable is 1 Value of local variable is 2 Value of local variable is 3 Value of local variable is 4 Value of local variable is 5 |
Ruby break statement:
Syntax:
Break
Terminate most internal loops. The method in the terminating Block returns nil if the called method and related block.
Instance:
?
1 2 3 4 5 6 7 8 |
#! /Usr/bin/ruby For I in 0 .. 5 If I> 2 then Break End Puts "Value of local variable is # {I }" End |
This produces the following results:
?
1 2 3 |
Value of local variable is 0 Value of local variable is 1 Value of local variable is 2 |
Ruby next statement:
Syntax:
Next
Jump to the next iteration of the most internal loop. If the call block terminates execution within one block (with yield or the call returns nil ).
Example:
?
1 2 3 4 5 6 7 8 |
#! /Usr/bin/ruby For I in 0 .. 5 If I <2 then Next End Puts "Value of local variable is # {I }" End |
This produces the following results:
?
1 2 3 4 |
Value of local variable is 2 Value of local variable is 3 Value of local variable is 4 Value of local variable is 5 |
Ruby redo statement:
Syntax:
Redo
Starts the innermost loop iteration again without checking the cycle conditions.
Yield or call will be restarted if one block is called.
Example:
?
1 2 3 4 5 6 7 8 |
#! /Usr/bin/ruby For I in 0 .. 5 If I <2 then Puts "Value of local variable is # {I }" Redo End End |
This will generate the following results and execute an infinite loop:
?
1 2 3 |
Value of local variable is 0 Value of local variable is 0 ............................ |
Ruby retry statement:
Syntax:
Retry
If the retry expression appears in the rescue clause, it starts from the beginning.
?
1 2 3 4 5 6 |
Begin Do_something # exception raised Rescue # Handles error Retry # restart from beginning End |
If retry iteration, block, or body expression occurs, restart the iteration call. The parameter conditions of the iterator are recalculated.
?
1 2 3 |
For I in 1 .. 5 Retry if some_condition # restart from I = 1 End |
Instance:
?
1 2 3 4 5 6 |
#! /Usr/bin/ruby For I in 1 .. 5 Retry if I> 2 Puts "Value of local variable is # {I }" End |
This will generate the following results and will enter an infinite loop:
?
1 2 3 4 5 6 7 |
Value of local variable is 1 Value of local variable is 2 Value of local variable is 1 Value of local variable is 2 Value of local variable is 1 Value of local variable is 2 ............................ |