ADVERTISEMENT
ADVERTISEMENT

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 »


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.