Ruby program to demonstrate the 'redo' statement in the loop

Ruby Example: Write a program to demonstrate the "redo" statement in the loop.
Submitted by Nidhi, on December 22, 2021

Problem Solution:

In this program, we will use the "redo" statement to repeat the current iteration of the loop. The redo statement has executed the body of the loop without evaluating the condition of the loop.

Program/Source Code:

The source code to demonstrate the "redo" statement in the loop is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the 
# "redo" statement in the loop

cnt = 0;

while(cnt < 5)  
  print cnt,": Hello World\n"  
  cnt = cnt + 1;   
  redo if cnt == 5 
end

Output:

0: Hello World
1: Hello World
2: Hello World
3: Hello World
4: Hello World
5: Hello World

Explanation:

In the above program, we used the "redo" statement to repeat the current iteration of the loop. The redo statement has executed the body of the loop without evaluating the condition of the loop.

Here we repeated the loop iteration when the value of cnt is 5 without checking the loop condition.

Ruby Looping Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.