MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to Break[] out of nested loops?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21865] Re: How to Break[] out of nested loops?
  • From: Ed McBride <emcbride at wybron.com>
  • Date: Wed, 2 Feb 2000 22:54:21 -0500 (EST)
  • Organization: Wybron, Inc.
  • References: <86r7bm$b72@smc.vnet.net> <870vkl$8ni@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com


Allan Hayes wrote:
> 
> "Julian Aguirre" <mtpagesj at lg.ehu.es> wrote in message
> news:86r7bm$b72 at smc.vnet.net...
> > In a loop like
> >
> > Do[
> > x=LengthyComputation[m];
> >     Do[
> >        If[condition[x,n], Break[]],
> >     {n, nmax}],
> > {m, mmax}]
> >
> > the Break[] exits the inner loop. Is there a way of exiting out of the
> outer
> > loop? I do not want to write like
> >
> > Do[
> > x=LengthyComputation[m];
> > If[condition[x,n], Break[]],
> > {m, mmax},{n, nmax}]
> >
> > because then LengthyComputation[m] is calculated nmax times for each value
> > of m isnstead of just once.
> >
> > Thanks in advance.
> >
> > --
> > Julian Aguirre
> > Universidad del Pais Vasco
> >
> >
> 
> Julian:
> 
> Suggestion: use Throw and Catch:
> 
> x = 0;
> Do[x = m^2;
>     Do[If[2n + x > 30, Throw[Null]], {n, 5}], {m, 7}] // Catch
> 
> Allan
> ---------------------

Or, if you happen to be an organized, disciplined person who does not
rank go-to-type statements right up there with sacrilege, you could even
stoop to a "Goto" statement.  I mean, if God didn't want us to use this
statement, why would Mr. Wolfram have been 
permitted to include it in his software package?
Ed McBride, P.E.


  • Prev by Date: Re: Fitting a function to a list (newbie)
  • Next by Date: Re: NDSolve and numeric-only computation
  • Previous by thread: Re: Cellular Automaton in Mathematica and OOP
  • Next by thread: Re: How to Break[] out of nested loops?