MathGroup Archive 2004

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

Search the Archive

Re: First question

  • To: mathgroup at
  • Subject: [mg52107] Re: [mg52091] First question
  • From: "David Annetts" <davidannetts at>
  • Date: Thu, 11 Nov 2004 04:52:08 -0500 (EST)
  • Sender: owner-wri-mathgroup at

Hi Gilbert,

> 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"}]
> ]

You have a bug in your code, as well as some possible misunderstanding, and
certainly some dangerous programming.

The bug is the If statement which should read
If[i > 3,
  Return[{"from inside"}],
  Return[{"not from inside"}]

Your final statement means that the function will always return "not from
inside".  There is a confusion since you may have another i which is global
that you want to work from instead.

The dangerous programming is that i is a local variable to the loop.  It
always takes values 1, 2, 3, 4, 5.  Which in turn means that it returns the
last value (there are no Break[]'s in your code) which is 5.

You have some reading to do ....



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