Mathematica 9 is now available
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

Where does Return return to?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg48601] Where does Return return to?
  • From: koopman at sfu.ca (Ray Koopman)
  • Date: Mon, 7 Jun 2004 05:33:29 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

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?