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: Re: First question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52149] Re: [mg52107] Re: [mg52091] First question
  • From: DrBob <drbob at bigfoot.com>
  • Date: Fri, 12 Nov 2004 02:14:06 -0500 (EST)
  • References: <200411110952.EAA28769@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

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

No, that would stop Do on its first iteration; clearly not what the OP intended.

Bobby

On Thu, 11 Nov 2004 04:52:08 -0500 (EST), David Annetts <davidannetts at ihug.com.au> wrote:

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



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: again: nonlinear fit...
  • Next by Date: Re: equal distribution of last digits base ten in the primes by b-normality
  • Previous by thread: Re: First question
  • Next by thread: Re: First question