MathGroup Archive 2005

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

Search the Archive

Re: goto and label (cont)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57814] Re: goto and label (cont)
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Thu, 9 Jun 2005 05:17:49 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

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

>as you see i do something1 then check some cond. if something
>didn't go well i start doing everything from the start. same for
>something2. I want to go all the way to the top. using a while or
>something alike would need some variable to hold it and maintaine
>which is messy. or if a do while loop exists (?) it will need to do
>everything and then restart. I want some mechanism that will
>restart itself if conditions are not met. goto seems the only
>simple option to do it.

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[]]]

With this, the while loop continues until test evaluated true and causes a Break[] to be executed. At that time the loop is exited. And since the do something comes before the If, you are certain to do something at least once.

Do note, this simply shows you how to achieve the flow control you want without a goto. It is still an example of proceedural programing. It is almost always the case, Mathematica works more efficiently and faster if functional programming is used rather than proceedural programming. And it is usually true there is a functional approach for any proceedural approach.

I've no way to suggest a functional approach without a better definition of the problem you are trying to solve.
--
To reply via email subtract one hundred and four


  • Prev by Date: Remove definitions from I
  • Next by Date: Re: sorting complex number?
  • Previous by thread: goto and label (cont)
  • Next by thread: Re: goto and label (cont)