MathGroup Archive 2004

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

Search the Archive

Re: Re: Where does Return return to?

  • To: mathgroup at
  • Subject: [mg48635] Re: [mg48629] Re: Where does Return return to?
  • From: Andrzej Kozlowski <akoz at>
  • Date: Wed, 9 Jun 2004 04:16:52 -0400 (EDT)
  • References: <ca1d48$d0r$> <>
  • Sender: owner-wri-mathgroup at

On 8 Jun 2004, at 13:48, Ray Koopman wrote:

> This may make the problem clearer.
> Why does Return exit Block in func1 but not in func2 or func3?
> In[1]:= func1 := Block[{L = 2},
>                  While[L > 1, If[True, Return[1]]; L--]; 0]
> In[2]:= func2 := Block[{L = 2},
>                  While[If[True, Return[1]]; --L > 1]; 0]
> In[3]:= func3 := Block[{},
>                  Do[If[True, Return[1]], {L,2,2,-1}]; 0]
> In[4]:= func1
> Out[4]= 1
> In[5]:= func2
> Out[5]= 0
> In[6]:= func3
> Out[6]= 0

I will explain func1 and func3. I do not want even to consider func2 as 
I never use this syntax, I see no point in its existence, and in fact I 
think it is a nuisance that ought to be got rid of.

As for the first two cases, compare

While[True, Return[1]]




I think this makes it clear. The While loop returns Return[1], which 
carries over to the encompassing control structure. The Do loop returns 
only 1, which means the encompassing control structure will not be 
exited. Note that:



As for the reason for the difference, it seems to me (its only my 
speculation of course) that the loops are intended to be used in a 
different way, with a Do loop usually use by itself while the While 
loop more likely to be used inside the body of a function. In 
connection with this last point note that:

f[x_]:= Return[x]

will give





just returns


Andrzej Kozlowski
Chiba, Japan

  • Prev by Date: Re: Graphic looping error?
  • Next by Date: RE: Combining plots
  • Previous by thread: Re: Where does Return return to?
  • Next by thread: Re: Where does Return return to?