Table of contents
1.
Introduction
2.
The next statement
2.1.
Syntax
2.2.
Example - 1
2.3.
Explanation
2.4.
Example - 2
2.5.
Explanation
3.
Frequently Asked Questions
3.1.
Is it possible to alter the control flow in ruby?
3.2.
What is control flow in Ruby?
3.3.
What is an iterator in Ruby?
3.4.
What does next do in Ruby?
3.5.
What does break return in Ruby?
4.
Conclusion
Last Updated: Mar 27, 2024

Next: Altering the Control Flow

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In addition to loops, conditionals, and iterators, the Ruby programming language has various statements to alter the control flow in a program. In other words, these statements are a piece of code that executes one after the other until the condition is true, and then the code terminates.

The following statements can alter the control flow in a Ruby program:
 

  1. The break statement
  2. The next statement
  3. The redo statement
  4. The retry statement
  5. The return statement
  6. The throw/catch statement

 

In this blog, we will discuss altering the control flow using the next statement.

So, let us get started: 

Start Image

The next statement

We utilise the next statement to skip the remainder of the current iteration. There won't be any further iterations after the next statement is executed. In every other language, a next statement is equivalent to a “continue” statement.

To be precise, the next statement skips the current iteration of the loop and begins the next iteration.

Let us see the syntax for altering the control flow using the next statement.

Syntax

next
You can also try this code with Online Ruby Compiler
Run Code

Example - 1

The codes given below show altering the control flow using the next statement.

# Ruby program for altering the control flow using the next statement 
for x  in  1..7
  
        # Used condition
         if  x < 3 then
  
            # Using the next statement
            next
         end
  
         # Printing values
         puts "Value of x is : #{x}"
      End
You can also try this code with Online Ruby Compiler
Run Code

 

Output

The code above produces the output given below:

Output

Explanation

In the above example, we are checking if the variable x is smaller than 3 or not. If yes, then skip the iteration by using the next keyword. 

Once x becomes greater than 3, print its value. 

Explanation

Let us see another example that shows altering the control flow using the next statement.

Example - 2

# Ruby program for altering the control flow using the next statement

i = 0   
while true   
    if i*5 >= 20   
        next   
    end   
    puts i*5   
    i += 1   
end 
You can also try this code with Online Ruby Compiler
Run Code

  

Output

The code above gives the following output:

Output

Explanation

In the above example, we are checking if the variable the statement i * 5 is greater than 20 or not. Using the next keyword, we are stopping our program to go beyond the value of 20. Hence, it always prints values less than 20. 

Frequently Asked Questions

Is it possible to alter the control flow in ruby?

On the other hand, two control flow statements allow you to adjust the control flow. The continue directs the control flow to the loop condition (for the while and the do-while loops) or the update condition (for the for loops). Control flow jumps to the following iteration as a result of this.

What is control flow in Ruby?

A control flow construct is a language feature that interrupts the regular progression to the next statement by branching to another position in the source code, either conditionally or unconditionally.

What is an iterator in Ruby?

In Ruby, the object-oriented idea is known as "iterators." Iterators, to put it another way, are the techniques supported by collections (Arrays, Hashes etc.). The objects that store a set of data members are known as collections. Iterators in Ruby return all the items of a collection one by one.

What does next do in Ruby?

To skip the loop's subsequent iteration, use the next command in Ruby. No more iterations will be made after the following statement has been executed. In other languages, the “continue” statement is analogous to the next statement in Ruby.

What does break return in Ruby?

Within a loop, the break is invoked. You'll be placed just outside of the innermost loop you're in. When a method is called, the return is invoked. It will put you immediately after where it was called and return the value you instruct it to.

Conclusion

In this article, we have extensively discussed altering the control flow using the next statement and its code in ruby.

After reading about altering the control flow using the next statement and their code in ruby, are you not feeling excited to read/explore more articles on the topic of DSA? Don't worry; Coding Ninjas has you covered. To learn, see Try Ruby, the History of Ruby and Constants in Ruby.
 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.
 

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Ninjas logo

Live masterclass