How to break out deeply nested loops? (newbie)

T

Talha Oktay

------=_Part_7267_58209.1143742564044
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I sometimes need to break out, next or redo deeply nested loops in differen=
t
levels instead of the innermost enclosing scope. Is there a facility to
label blocks or loops and redo, next or break to the labeled blocks or
loops as in some other programming languages?

Thanks in advance.

------=_Part_7267_58209.1143742564044--
 
C

Chris Alfeld

Not nicely.

This is discussed in the Pickaxe. For example

catch "BreakOuterLoop" do
for i in 1..10
print "out #{i}\n"
for j in 1..10
print "in #{j}\n"
throw "BreakOuterLoop" if i+j > 16
end
end
end
 
S

Simon Kröger

Talha said:
I sometimes need to break out, next or redo deeply nested loops in different
levels instead of the innermost enclosing scope. Is there a facility to
label blocks or loops and redo, next or break to the labeled blocks or
loops as in some other programming languages?

Thanks in advance.


You may use throw, catch or continuations (for the fun of it) or just
wrap some of your inner loops in their own method and use return.
(i prefer the last version most of the time)

cheers

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top