MathGroup Archive 2005

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

Search the Archive

Re: goto and label (cont)

  • To: mathgroup at
  • Subject: [mg57831] Re: [mg57768] goto and label (cont)
  • From: "Ingolf Dahl" <ingolf.dahl at>
  • Date: Thu, 9 Jun 2005 05:55:32 -0400 (EDT)
  • Organization: Goteborg University
  • Reply-to: <ingolf.dahl at>
  • Sender: owner-wri-mathgroup at

Hi Gui,
The reason for us as humans to write computer code is to take control over
the action of computers. The reason to avoid the Goto command is to avoid
spaghetti code that is difficult to debug and to reinterpret later. The Goto
enables you to create a wormhole from any point at any level to any other
point at any other level in the program, and this means that there is no
clear "flow direction" in the program. Is this way the program easily gets
very complicated topology. When you try to read such a program, and find a
label, you must check the complete code for Goto commands, and understand
the state of the variables at the jump, to be able to see the effects of the
commands following the label. I have had difficulties in rewriting my own
old Basic programs as Mathematica code just because these goto commands. I
also imagine that compilers can have difficulties to create optimal code
when Goto commands are used. Thus, as a general rule, avoid "Goto". As with
all such general rules, there might of course be exceptions.   
We can rewrite your pseudo code in the following way, to obtain one outer
and one inner loop, with only one Goto for each label.

do something1
if condition1==true goto[L1]
do something2
if condition2==true goto[L2]
do some other stuff

Here is some equivalent pseudo code, using While instead. With While, there
is automatically only one start and end point per jump. I redefine
condition1 and condition2 as variables, which can take the values True or

condition1 = True; condition2 = True;
	While[condition1, do something1; recalculate condition1];
   	do something2; condition1 = True; recalculate condition2]

Here is a concrete example:

x = 0; y = 0; condition1 = True; condition2 = True; 
	While[condition1, x = x + 1; condition1 = x <= 10]; 
	y = y + 1; condition1 = True; condition2 = (x <= 15 || y <= 10)]; 
{x, y}

(Returns {21,11})

Best regards

Ingolf Dahl

-----Original Message-----
From: Guy Israeli [mailto:guyi1 at] 
To: mathgroup at
Subject: [mg57831] [mg57768] goto and label (cont)

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

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.

again, suggestions are always welcomed

Thanks a lot,


  • Prev by Date: Re: Numbering equations
  • Next by Date: Re: string to a special list
  • Previous by thread: Re: goto and label (cont)
  • Next by thread: Re: goto and label (cont)