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
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:
i = 1
while true
if i * 6 >= 30
break
end
puts i * 6
i += 1
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:
for t in 0 ... 10
if t == 5 then
next
end
puts t
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:
val = 0
while (val < 4 )
puts val
val += 1
redo if val == 4
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:
var = 7
var.times do |val|
puts val
if val == 6
var = var - 1
retry
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:
def geeks
val1 = 61
val2 = 55
return val1, val2
puts "Hello Geeks"
end
value = geeks
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:
def lessNumber(num)
throw :numberError if num < 10
puts "Number is Greater than 10!"
end
catch :numberError do
lessNumber( 11 )
lessNumber( 78 )
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...