Ruby | Control Flow Alteration Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Prerequisite : Decision Making , Loops Ruby programming language provides some statements in addition to loops, conditionals, and iterators, which are used to change the flow of control in a program. In other words, these statements are a piece of code that executes one after another until the condition is true and when the condition becomes false then code terminated. The following are the statements which can alter the control flow in a Ruby program: break statement next statement redo statement retry statement return statement throw/catch statement break Statement In Ruby, Break statement is used to exit a loop when the condition is true. Break statement is basically used in while loop because in while loop the output is printed till the condition is true, when the condition is false the loop exited. The break statement is used inside the loop. The break statement are executed by break keyword. Break statement can also be used in for, while, and case control statements. Syntax: break Example: Ruby # Ruby program to illustrate break statement #!/usr/bin/ruby i = 1 # using while loop while true if i * 6 >= 30 # using break statement break # ending of if statement end puts i * 6 i += 1 # ending of while loop end Output: 6 12 18 24 Explanation: In above example, break statement is used to stop the execution of while loop when the condition if i * 6 >= 30 becomes true. Otherwise loop goes to infinite. next Statement In Ruby, next statement is used to jump to the next iterator of given loop. The next statement is identical to continue statement in C and Java language. When the next statement is used no other iteration will be performed. Generally, the next statement is used in for and while loop. Syntax: next Example: Ruby # Ruby program to illustrate next statement #!/usr/bin/ruby # using for loop for t in 0...10 # using if statement if t == 5 then # using next statement next # ending of if end # displaying values puts t # end of for loop end Output: 0 1 2 3 4 6 7 8 9 Explanation: In the above program, 5 will not be printed in the output due to next statement. So here at 5 next statement will cause to skip it and continue from next statement in program. redo Statement The redo statement is used to restart the current iteration of a loop or the iterator. There is a difference between the redo and next statement. next statement always transfers the control to the end of the loop where the statement after the loop can start to execute, but redo statement transfer the control back to the top of block or loop so that iteration can start over. Syntax: redo Example: Ruby # Ruby program to demonstrate the redo statement # defining a variable val = 0 # using while loop which should give # output as 0,1,2,3 but here it will # output as 0,1,2,3,4 while(val < 4) # Control returns here when # redo will execute puts val val += 1 # using redo statement redo if val == 4 # ending of while loop end Output: 0 1 2 3 4 Explanation: In the above program, redo statement will transfer the control to puts val which is the first expression of while loop. It is neither going to retest the loop condition nor going to fetch the next element from the iterator. So, here while loop will print 0,1,2,3,4 instead of 0,1,2,3. retry Statement(deprecated in recent versions) retry statement is used to restart an iterator based on a certain condition or any method invocation from the starting. In simple words, retry statement transfers the control at the beginning. Generally, a retry statement is used rarely. It will only work till Ruby version 1.8. Note: retry statement has been removed from the Ruby version 1.9 onwards because it is considered deprecated language feature. So it will hardly run on online IDE's because mostly using version above 1.8. Syntax: retry Example: Ruby # Ruby program to demonstrate the retry statement # variable var = 7 # Iterate 7 times from 0 to 7-1 var.times do |val| # display iteration number puts val # If we've reached 6 if val == 6 # Decrement val and user won't # reach 6 next time var = var - 1 # Restart the iteration # using retry statement retry # end of if end # end of do..end end Output: 0 1 2 3 4 5 6 0 1 2 3 4 5 Explanation: In above program, when the control goes to retry statement then it transfers that control to var.times do |val| . Now here the value of var variable is updated i.e. 5. So user won't reach 6 to next time and retry statement will not execute again. return statement This is used to exit from a method, with or without a value. It always returns a value to its caller. There are many options with the return statement. If there is no expression used with return statement then it always returns the value of method as nil. A list of expression after the return statement is always separated by the comma(,) In this case, the value of the method will be an array containing the values of those specified expressions. Example: Ruby # Ruby program to demonstrate the return statement #!/usr/bin/ruby # defining a method 'geeks' def geeks # variables of method val1 = 61 val2 = 55 # returning multiple values return val1, val2 # this statement will not execute puts "Hello Geeks" # end of the method end # variable outside the method to # store the return value of the method value = geeks # displaying the returned values puts value Output: 55 61 Explanation: In above example, method geeks has a return statement which return the two values i.e. val1 and val2 to its caller. Here value is the variable which stored the returned values. The important point is that the statement puts "Hello Geeks" after return statement doesn't execute because statements after the return statement will not execute inside a method. throw/catch Statement throw and catch are used to define a control structure which can be considered as a multilevel break. throw is used to break the current loop and transfer the control outside of the catch block. The best thing about throw is that it can break out of the current loop or methods or we can say it can cross any number of levels. Here, catch defines a "labeled block" of code which causes to exit by the throw block. More details will be discussed In Ruby Exception Handling section. Example: Ruby # Ruby program to illustrate the throw/catch statement # for altering the control flow # defining a method def lessNumber(num) # using throw statement # here 'numberError' is its label throw :numberError if num < 10 # displaying result puts "Number is Greater than 10!" end # catch block catch :numberError do # calling method lessNumber(11) lessNumber(78) # exits catch block here lessNumber(7) lessNumber(4) end puts "Outside Catch Block" Output: Number is Greater than 10! Number is Greater than 10! Outside Catch Block Explanation: In above program, 11 is passed to method lessNumber to check whether it is greater than 10 or not. 11 is greater than 10 so statement will print out on display and next statement of catch block will execute. Now 78 is passed to the method call, which is checked and it is greater than 10 so statement will print out on screen. But as soon as 7 is passed which is less than 10 throw: numberError cause the catch block to exits and all the statements skip out and last statement "Outside Catch Block" will print. So here as soon as the condition becomes false throw causes the catch block to exit the catch block from execution. Comment A ankita_saini Follow Improve A ankita_saini Follow Improve Article Tags : Misc Ruby Ruby-Basics Explore Ruby Programming Language 4 min read OverviewRuby For Beginners 3 min read Ruby Programming Language (Introduction) 4 min read Comparison of Java with Other Programming Languages 4 min read Similarities and Differences between Ruby and C language 3 min read Similarities and Differences between Ruby and C++ 3 min read Environment Setup in Ruby 3 min read How to install Ruby on Linux? 2 min read How to install Ruby on Windows? 2 min read Interesting facts about Ruby Programming Language 2 min read BasicsRuby | Keywords 4 min read Ruby | Data Types 3 min read Ruby Basic Syntax 3 min read Hello World in Ruby 2 min read Ruby | Types of Variables 4 min read Global Variable in Ruby 2 min read Comments in Ruby 2 min read Ruby | Ranges 4 min read Ruby Literals 4 min read Ruby Directories 5 min read Ruby | Operators 11 min read Operator Precedence in Ruby 2 min read Operator Overloading in Ruby 5 min read Ruby | Pre-define Variables & Constants 5 min read Ruby | unless Statement and unless Modifier 2 min read Control StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1 3 min read Ruby | Loops (for, while, do..while, until) 5 min read Ruby | Case Statement 3 min read Ruby | Control Flow Alteration 7 min read Ruby Break and Next Statement 2 min read Ruby redo and retry Statement 2 min read BEGIN and END Blocks In Ruby 2 min read File Handling in Ruby 4 min read MethodsRuby | Methods 3 min read Method Visibility in Ruby 3 min read Recursion in Ruby 4 min read Ruby Hook Methods 5 min read Ruby | Range Class Methods 5 min read The Initialize Method in Ruby 2 min read Ruby | Method overriding 2 min read Ruby Date and Time 3 min read OOP ConceptsObject-Oriented Programming in Ruby | Set 1 9 min read Object Oriented Programming in Ruby | Set-2 8 min read Ruby | Class & Object 4 min read Private Classes in Ruby 3 min read Freezing Objects | Ruby 2 min read Ruby | Inheritance 4 min read Polymorphism in Ruby 3 min read Ruby | Constructors 2 min read Ruby | Access Control 8 min read Ruby | Encapsulation 2 min read Ruby Mixins 3 min read Instance Variables in Ruby 3 min read Data Abstraction in Ruby 3 min read Ruby Static Members 3 min read ExceptionsRuby | Exceptions 4 min read Ruby | Exception handling 6 min read Catch and Throw Exception In Ruby 3 min read Raising Exceptions in Ruby 4 min read Ruby | Exception Handling in Threads | Set - 1 2 min read Ruby | Exception Class and its Methods 3 min read Ruby RegexRuby | Regular Expressions 3 min read Ruby Search and Replace 2 min read Ruby ClassesRuby | Float Class 7 min read Ruby | Integer Class 3 min read Ruby | Symbol Class 5 min read Ruby | Struct Class 5 min read Ruby | Dir Class and its methods 3 min read Ruby | MatchData Class 4 min read Ruby ModuleRuby | Module 4 min read Ruby | Comparable Module 3 min read Ruby | Math Module 4 min read Include v/s Extend in Ruby 2 min read Like