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.
- Follow-Ups:
- Re: Where does Return return to?
- From: DrBob <drbob@bigfoot.com>
- Re: Where does Return return to?