# Break statement in Loop

**URL:** <https://www.ruby-forum.com/t/break-statement-in-loop/239224>\
**Category:** Ruby\
**Created:** [February 18, 2014, 8:25am UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224 "2014-02-18T08:25:00Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [February 18, 2014, 8:25am UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/1 "2014-02-18T08:25:00Z")

</div>

a=[[1,2,3],[2,3,4],[54,6,5]]

for i in a  
for j in i  
puts j  
if j.eql?2  
break  
end  
end  
end

This break breaks the inner loop, but I would like to get out of the  
both loop once condition is reached. Can I do this without using flag  
kind of thing?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 18, 2014, 8:49am UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/2 "2014-02-18T08:49:09Z")

</div>

Raja gopalan wrote in post #1136996:

> a=[[1,2,3],[2,3,4],[54,6,5]]

> This break breaks the inner loop, but I would like to get out of the  
> both loop once condition is reached. Can I do this without using flag  
> kind of thing?

I’d do as below :

a=[[1,2,3],[2,3,4],[54,6,5]]  
prc = proc { break }  
for i in a  
for j in i  
puts j  
if j.eql?2  
prc.call  
end  
end  
end

**output**  
1  
2

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 18, 2014, 8:55am UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/3 "2014-02-18T08:55:38Z")

</div>

hi Arup R.

It’s not working for me,

My output is this

1  
2  
3  
2  
3  
4  
54  
6  
5

I am using Ruby 1.8.7 that might be cause of this?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 18, 2014, 9:16am UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/4 "2014-02-18T09:16:57Z")

</div>

ok Thank you Arup R.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 18, 2014, 9:04am UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/5 "2014-02-18T09:04:45Z")

</div>

Raja gopalan wrote in post #1136999:

> hi Arup R.
> 
> It’s not working for me,

May be. My version is -

C:\ruby\>ruby -v hello.rb  
ruby 1.9.3p448 (2013-06-27) [i386-mingw32]  
1  
2

C:\ruby\>

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 18, 2014, 10:18pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/6 "2014-02-18T22:18:48Z")

</div>

Am 18.02.2014 08:49, schrieb Arup R.:

> prc = proc { break }  
> 1  
> 2

This is very unclear, and in 1.8 proc behaves differently.

Introducing a method and using return instead of break would be  
much clearer (see my other post).

Also possible: throw and catch

a = [[1,2,3], [2,3,4], [54,6,5]]

catch :quit do  
a.each do |i|  
i.each do |j|  
puts j  
throw(:quit) if j == 2  
end  
end  
end

Regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 18, 2014, 9:46pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/7 "2014-02-18T21:46:08Z")

</div>

Am 18.02.2014 08:25, schrieb Raja gopalan:

> a=[[1,2,3],[2,3,4],[54,6,5]]
> 
> for i in a  
> for j in i  
> puts j  
> if j.eql?2  
> break  
> end  
> end  
> end

First, I would write this like:

a = [[1,2,3], [2,3,4], [54,6,5]]

a.each do |i|  
i.each do |j|  
puts j  
break if j == 2  
end  
end

(actually, I also wouldn’t use one-letter variable names)

> This break breaks the inner loop, but I would like to get out of the  
> both loop once condition is reached. Can I do this without using flag  
> kind of thing?

Second, you can achieve this by wrapping your code in a method  
and using return:

def handle(array)  
array.each do |i|  
i.each do |j|  
puts j  
return if j == 2  
end  
end  
end

a = [[1,2,3], [2,3,4], [54,6,5]]  
handle(a)

Output:  
1  
2

Regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 18, 2014, 10:32pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/8 "2014-02-18T22:32:04Z")

</div>

unknown wrote in post #1137119:

> Am 18.02.2014 08:49, schrieb Arup R.:
> 
> > prc = proc { break }  
> > 1  
> > 2
> 
> This is very unclear, and in 1.8 proc behaves differently.

Thanks for letting me know.

> Introducing a method and using return instead of break would be  
> much clearer (see my other post).

Yes, that was nice. I actually wanted to show OP, how to break out from  
the loop.

> Also possible: throw and catch
> 
> a = [[1,2,3], [2,3,4], [54,6,5]]
> 
> catch :quit do  
> a.each do |i|  
> i.each do |j|  
> puts j  
> throw(:quit) if j == 2  
> end  
> end  
> end

Nice learning for me.

> Regards,  
> Marcus

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 11:43am UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/9 "2014-02-19T11:43:11Z")

</div>

On Tue, Feb 18, 2014 at 8:25 AM, Raja gopalan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This break breaks the inner loop, but I would like to get out of the  
> both loop once condition is reached. Can I do this without using flag  
> kind of thing?

You can use catch throw for this:

catch :outer do  
for i in a  
for j in i  
puts j  
if j.eql?2  
throw :outer  
end  
end  
end  
end

Cheers

robert

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 18, 2014, 11:01pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/10 "2014-02-18T23:01:12Z")

</div>

On Mon, Feb 17, 2014 at 11:25 PM, Raja gopalan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This break breaks the inner loop, but I would like to get out of the  
> both loop once condition is reached. Can I do this without using flag  
> kind of thing?

I don’t see the point of two loops for this, so how about –

a = [[1,2,3], [2,3,4], [54,6,5]].flatten  
( puts (x = a.shift); break if x.eql?(2) ) until a.empty?

FWIW,

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 12:52pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/11 "2014-02-19T12:52:40Z")

</div>

Robert K. wrote in post #1137187:

> On Tue, Feb 18, 2014 at 8:25 AM, Raja gopalan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:

> catch :outer do  
> for i in a  
> for j in i  
> puts j  
> if j.eql?2  
> throw :outer  
> end  
> end  
> end  
> end

+1 for teaching this one to me. 🙂

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 12:16pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/12 "2014-02-19T12:16:18Z")

</div>

hi Hassan S.,

You code works, but my intention was to have two loops and breaking two  
loops when the condition is met. Thank you.

hi Robert K.,

Your Code ROCKS!!!, This is what I expected and I am not even aware of  
these type of coding ‘catch :outer do’. Thank you.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 1:02pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/13 "2014-02-19T13:02:03Z")

</div>

On Wed, Feb 19, 2014 at 12:16 PM, Raja gopalan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> hi Hassan S.,
> 
> You code works, but my intention was to have two loops and breaking two  
> loops when the condition is met. Thank you.
> 
> hi Robert K.,
> 
> Your Code ROCKS!!!, This is what I expected and I am not even aware of  
> these type of coding ‘catch :outer do’. Thank you.

You’re welcome! You can even use this to transport information:

irb(main):001:0\> x = catch(:foo) { throw :foo }  
=\> nil  
irb(main):002:0\> x  
=\> nil  
irb(main):003:0\> x = catch(:foo) { throw :foo, 123 }  
=\> 123  
irb(main):004:0\> x  
=\> 123

Cheers

robert

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 1:41pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/14 "2014-02-19T13:41:21Z")

</div>

Robert K. wrote in post #1137197:

> You’re welcome! You can even use this to transport information:
> 
> irb(main):001:0\> x = catch(:foo) { throw :foo }  
> =\> nil  
> irb(main):002:0\> x  
> =\> nil  
> irb(main):003:0\> x = catch(:foo) { throw :foo, 123 }  
> =\> 123  
> irb(main):004:0\> x  
> =\> 123

Again I went to the doc link -  
[Module: Kernel (Ruby 2.1.0)](http://www.ruby-doc.org/core-2.1.0/Kernel.html#method-i-catch) . I  
understood what happened with above code.

From the doc one line I don’t understand - when no **arg** is given,  
`catch` assigns a **new unique object** to throw. this is useful for  
**nested catch**.

Can you explain this with a example ?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 1:59pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/15 "2014-02-19T13:59:46Z")

</div>

In general, it is considered bad practice to use exceptions for flow  
control.  
Exceptions indicate, and should be used for, cases where something  
unexpected happened and you are trying to recover.  
I strongly encourage you not to use them in this way.

The have seen several good alternatives to your problem:  
\* flatten the array and use one loop  
\* make a method and use return

“ruby-talk” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote on 02/19/2014  
07:41:21  
AM:

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 1:57pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/16 "2014-02-19T13:57:28Z")

</div>

Robert K. wrote in post #1137197:

> irb(main):001:0\> x = catch(:foo) { throw :foo }  
> =\> nil  
> irb(main):002:0\> x  
> =\> nil  
> irb(main):003:0\> x = catch(:foo) { throw :foo, 123 }  
> =\> 123  
> irb(main):004:0\> x  
> =\> 123
> 
> Cheers
> 
> robert

Ok Thank you.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 2:19pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/17 "2014-02-19T14:19:13Z")

</div>

On Wed, Feb 19, 2014 at 1:59 PM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In general, it is considered bad practice to use exceptions for flow  
> control.  
> Exceptions indicate, and should be used for, cases where something  
> unexpected happened and you are trying to recover.

These are no exceptions. Notice this:

Exceptions: raise…rescue  
Flow control: catch…throw

> I strongly encourage you not to use them in this way.

In Ruby this was invented intentionally to allow for such kind of _flow  
control_. I agree, one should use is sparingly but there are situations  
where it comes handy. Personally I can’t remember the last time I used  
it  
though. 🙂

> The have seen several good alternatives to your problem:  
> \* flatten the array and use one loop  
> \* make a method and use return

There’s also another one

irb(main):001:0\> a=[[1,2,3],[2,3,4],[54,6,5]]  
=\> [[1, 2, 3], [2, 3, 4], [54, 6, 5]]  
irb(main):002:0\> a.each {|i| i.each {|j| p j; break if j == 2} or break}  
1  
2  
=\> nil

Works also if the element is not found in the first sub array:

irb(main):006:0\> a.each {|i| i.each {|j| p j; break if j == 4} or break}  
1  
2  
3  
2  
3  
4  
=\> nil

You can see that it completely iterates if the condition does not match:

irb(main):007:0\> a.each {|i| i.each {|j| p j; break if j == 999} or  
break}  
1  
2  
3  
2  
3  
4  
54  
6  
5  
=\> [[1, 2, 3], [2, 3, 4], [54, 6, 5]]

If we learn more about the problem that is to be solved here we may come  
up  
with even better solutions.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 19, 2014, 2:45pm UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/18 "2014-02-19T14:45:49Z")

</div>

hi Robert K.,

“a.each {|i| i.each {|j| p j; break if j == 2} or break}”

This is an another interesting code. You really Rock with Ruby CODE!!!  
But I don’t understand how this ‘or break’ works? See the inner each  
loop returns the enumerator So Is the ‘or operation’ performed with  
enumerator object?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [February 22, 2014, 4:56am UTC](https://www.ruby-forum.com/t/break-statement-in-loop/239224/19 "2014-02-22T04:56:06Z")

</div>

Dear Arup,

Basically instead of

catch(:foo) { throw :foo }

You can write

catch { |foo| throw foo }

Look again:

catch(:foo) { |arg| p arg; throw arg }

# :foo

# =\> nil

catch { |arg| p arg; throw arg }

# #Object:0x000000025ab778

# =\> nil

It creates for you (and for free) a unique instance of Object and  
yields it to the block.

I couldn’t come out with a good code example where this could be useful.  
At least we have two stylist way of writing it! 😉

> **[Bug #1618: Kernel.catch without Arguments - Ruby - Ruby Issue Tracking System](https://bugs.ruby-lang.org/issues/1618)**
>
> Redmine

[https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/23675/diff/](https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/23675/diff/)

Best regards,  
Abinoam Jr.
