Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now