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: [mg21801] Re: [mg21790] How to Break[] out of nested loops?
  • From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
  • Date: Fri, 28 Jan 2000 01:45:53 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

The best way, I think, is to use Catch and Throw instead of Break, e.g.

Catch[Do[
x=LengthyComputation[m];
    Do[
       If[condition[x,n], Throw[something]],
    {n, nmax}],
{m, mmax}]]

Throw and Catch admit additional arguments which give you a complete control
over which part of a multiple loop you exit.

> From: Julian Aguirre <mtpagesj at lg.ehu.es>
To: mathgroup at smc.vnet.net
> Organization: Universidad del Pais Vasco/Euskal Herriko Unibertsitatea
> Date: Thu, 27 Jan 2000 22:57:25 -0500 (EST)
> To: mathgroup at smc.vnet.net
> Subject: [mg21801] [mg21790] How to Break[] out of nested loops?
> 
> 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
> 
> 



  • Prev by Date: Re: Could this be improved?---Continued
  • Next by Date: Re: notebook
  • Previous by thread: Re: How to Break[] out of nested loops?
  • Next by thread: Partial Differential Equations