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.
- Follow-Ups:
- Re: Re: First question
- From: DrBob <drbob@bigfoot.com>
- Re: Re: First question