MathGroup Archive 2004

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

Search the Archive

Where does Return return to?


Consider the following three functions,
which I thought would be equivalent.

func1[n_] := Block[{L = n, otherstuff},
             While[L > 1, 
                   Which[cond1, expr1,
                         cond2, Return[expr2],
                         True,  expr3];
                   L--];
             expr4]

func2[n_] := Block[{L = n, otherstuff},
             While[Which[cond1, expr1,
                         cond2, Return[expr2],
                         True,  expr3];
                   --L > 1];
             expr4]

func3[n_] := Block[{otherstuff},
             Do[Which[cond1, expr1,
                      cond2, Return[expr2],
                      True,  expr3],
                {L,n,2,-1}];
             expr4]

func1 returns expr2 or expr4, as desired,
but both func2 and func3 always return expr4.

In each function, nothing changes if I replace the Which by

   If[cond1, expr1, If[cond2, Return[expr2], expr3]].

Yes, I know I could use Throw/Catch here, but on my system that's
slower, and it would still leave me wondering what the rule is for
where Return returns to.


  • Prev by Date: Re: SetPrecision - What does in find?
  • Next by Date: Re: Random Matrix of Integers
  • Previous by thread: Re: SetPrecision - What does in find?
  • Next by thread: Re: Where does Return return to?