MathGroup Archive 2005

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

Search the Archive

Re: Re: Do loops in Mathematica

  • To: mathgroup at
  • Subject: [mg55287] Re: [mg55174] Re: Do loops in Mathematica
  • From: DrBob <drbob at>
  • Date: Fri, 18 Mar 2005 05:34:11 -0500 (EST)
  • References: <d15s6o$9l2$> <> <> <>
  • Reply-to: drbob at
  • Sender: owner-wri-mathgroup at

One of the many reasons I never use Return is that you can't be sure where it will return FROM. It's probably not returning at all, in these examples:






In all three cases, the value of Do[...] should be 1 if Return exits Do, so it clearly doesn't. The first printed result should be 1 if Return exited List, no print at all if it exited from Print or Do. The last printed result shouldn't exist if Return exited Print or Do, and there's nothing else for it to exit.

This adheres to documentation (arguably) if we suppose List and Print are not loops or procedures, since Documentation says Return will "return the value expr, exiting all procedures and loops in a function". Does anyone know the official definition of "loop" or "procedure" or "control structure"? I certainly don't.

Anyway, don't use Return, and this will never affect you.

Throw and Catch are far more reliable.


On Thu, 17 Mar 2005 07:36:59 +0100, Daniel Huber <dh at> wrote:

> Hi Bob,
> sure your are right, x2 was a typo.
> However, there is something more serious.
> Please explain to me why the following should be considered a feature
> and not a bug:
> Do[Return[1],{i,1,1}]     does return 1
> Do[{Return[1]},{i,1,1}] does return Null
> Daniel
> DrBob wrote:
>> There is no bug, and...
>> This really works?
>> Do[p = Solve[x2 + 3*x + 1 == 0,
>>      x]; Return[2*p], {i, 1}]
>> {{2*(x -> (1/3)*(-1 - x2))}}

DrBob at

  • Prev by Date: Re: Re: Sum
  • Next by Date: Re: How can I compute the Fourier transform of a unit disk and a unit ball analytically by using Mathematica?
  • Previous by thread: Re: Re: Do loops in Mathematica
  • Next by thread: Re: Do loops in Mathematica