MathGroup Archive 2004

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

Search the Archive

Re: First question


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