Re: Nonstandard use of Return[] ??
- To: mathgroup at smc.vnet.net
- Subject: [mg3618] Re: Nonstandard use of Return[] ??
- From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
- Date: Thu, 28 Mar 1996 00:11:39 -0500
- Organization: University of Colorado, Boulder
- Sender: owner-wri-mathgroup at wolfram.com
In article <4j7h55$af4 at dragonfly.wolfram.com>, Lou Pecora <pecora at zoltar.nrl.navy.mil> wrote: >Try this: > >In:= >ff := ( Do[If[i<2,Return[1]],{i,3}]; Return[0]) > >In:= >ff > >You'll get 0 and not 1. Unlike the usual computer programming languages. >Strange choice of behavior. However, I'm sure it's locked into >Mathematica by now. > >Just a warning so others don't spend an hour debugging. > >Does anyone have a good way to exit the function totally? I seem to >remember some other kind of return once, but I can't find it. You can use Throw and Catch to cause a non-local return: (Local) In[9]:= gg := Catch[ Do[If[i<2,Throw[1]],{i,3}]; Throw[0] ] (Local) In[10]:= gg (Local) Out[10]= 1 Dave Wagner Principia Consulting (303) 786-8371 dbwagner at princon.com http://www.princon.com/princon ==== [MESSAGE SEPARATOR] ====