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

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

Problem Solution:

In this program, we will use the "next" statement to skip statement execution within the loop body. The "next" statement is similar to the 'continue' statement in the C language.

Program/Source Code:

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

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

for cnt in 1..10   
   if cnt == 5 then   
      next   
   end   
   print cnt," ";   
end

Output:

1 2 3 4 6 7 8 9 10

Explanation:

In the above program, we used the "next" statement to skip the execution of statements within the body of the loop. Here we skipped the printing of number 5 using the "next" statement.

Ruby Looping Programs »




Comments and Discussions!

Load comments ↻





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