MathGroup Archive 2004

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

Search the Archive

Re: First question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52101] Re: [mg52091] First question
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Thu, 11 Nov 2004 04:52:00 -0500 (EST)
  • References: <200411100945.EAA11265@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 10 Nov 2004, at 18:45, dto_mba at skynet.be wrote:

>
> How to exit directly and immediately a module from inside a loop with
> a certain value ?
> Return seems not to work.
>
> Example:
>
> lmaa := Module[
> {},
>    Do[
>      If[i > 3, Return[{"from inside"}]],
>      {i, 1, 5}
>    ];
>    Return[{"not from inside"}]
> ]
>
>
> lmaa
>
> {not from inside}
>
> Best
>
> Gilbert Gosseyn
>
>

Method 1 (not really recommended)
In[70]:=
lmaa := Module[
{},
    Do[
      If[i > 3, Return[Return[{"from inside"}]]],
      {i, 1, 5}
    ];
    Return[{"not from inside"}]
]



lmaa


{from inside}

Method 2 (recommended):


lmaa := Module[
{},
   Catch[ Do[
      If[i > 3, Throw[{"from inside"}]],
      {i, 1, 5}
    ];
    Return[{"not from inside"}]]
]



lmaa


{from inside}


Finally, there is absolutely no point using a Module whiteout any local 
variables.



Andrzej Kozlowski
Chiba, Japan
http://www.akikoz.net/~andrzej/
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: Re: First question
  • Next by Date: Re: Re: Poles and Complex Factoring
  • Previous by thread: Re: First question
  • Next by thread: Re: First question