Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: First question

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

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 ....

Regards,

Dave.




  • 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