Re: goto and label (cont)
- To: mathgroup at smc.vnet.net
- Subject: [mg57847] Re: goto and label (cont)
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Fri, 10 Jun 2005 02:29:10 -0400 (EDT)
- Organization: University of Washington
- References: <d892ej$sa3$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Bill Rowe" <readnewsciv at earthlink.net> wrote in message news:d892ej$sa3$1 at smc.vnet.net... > On 6/8/05 at 3:21 AM, guyi1 at netvision.net.il (Guy Israeli) wrote: > >>Hi again, >> >>since everyone tells me not to use goto and label (although for the >>use i'll describe below it is very comfterable and the simplest way >>to do it. If anyone can suggests an alternative/mathematica way of >>doing it, please do. > >>the pseudocode with goto > >>label[l] >>do something1 >>if condition1==true goto[l] >>do something2 >>if condition2==true goto[l] >>do some other stuff > [snip] > While loops will do what you want. The flow you describe above could be > coded as > > While[condition1 == True || condition2 == True, > While[condition1 == True, do something1]; > do something2] > do some other stuff > > The same functionality as a do while loop can be implemented in > Mathematica as a While loop as follows: > > While[True, > do something; > If[test, Break[]]] > A simpler way to get the functionality of a do while loop is to use the single argument form of While: While[ do something; test] Ray Koopman used this construction in a very nice way to get the functionality of Guy's spaghetti code. I quote his construction below: > > The logical OR evaluates its arguments in order, skipping subsequent > arguments as soon as a True result is found. Use it in a bodyless > While: > > While[(something1; condition1) || (something2; condition2)]; > otherstuff > Carl Woll [snip]