ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.