Thread Subject: a goto command???

Subject: a goto command???

From: Josh Hoppenjans

Date: 20 Jan, 2006 12:12:45

Message: 1 of 44

i was wondering if there was anything like a goto command.
i am trying to go from the end of and if statement back to the
beginning of the program.

Subject: a goto command???

From: tpl@eng.cam.ac.uk (Tim Love)

Date: 20 Jan, 2006 17:03:54

Message: 2 of 44

"Josh Hoppenjans" <jh333@evansville.edu> writes:

>i was wondering if there was anything like a goto command.
>i am trying to go from the end of and if statement back to the
>beginning of the program.
Read about "while"

Subject: a goto command???

From: Duane Hanselman

Date: 20 Jan, 2006 13:04:59

Message: 3 of 44

berniecr@clarkson.edu wrote:
>
>
> I wish Matlab had a goto command. I wanted one. I couldn't find
> it
> though. You have to restructure your code, and find ways around
> this
> stupid limitation.
>
>

Some would say that "goto" is a stupid command rather than saying its
absence is a stupid limitation. I'm an old FORTRAN programmer and I
don't miss it at all.

Why is it that you need "goto" ???

Subject: a goto command???

From: berniecr@clarkson.edu

Date: 20 Jan, 2006 11:08:37

Message: 4 of 44

a goto command can help you skip code. It could, in some circumstances,
make programming easier.

Subject: a goto command???

From: Steve Amphlett

Date: 20 Jan, 2006 15:10:11

Message: 5 of 44

berniecr@clarkson.edu wrote:
>
>
> a goto command can help you skip code. It could, in some
> circumstances,
> make programming easier.

goto: "somwhere I cannot come back from"

Subject: a goto command???

From: John D'Errico

Date: 20 Jan, 2006 15:33:28

Message: 6 of 44

berniecr@clarkson.edu wrote:
>
>
> a goto command can help you skip code. It could, in some
> circumstances,
> make programming easier.

Maintenance is part of programming. There are
many ways to make programming easier.

I'll dispute that goto is one of them.

Spend a few hours/dats/weeks/months trying to
debug spaghetti code created by liberal use of
goto statements and see if you still think its
easier.

John

Subject: a goto command???

From: CRet

Date: 20 Jan, 2006 21:31:16

Message: 7 of 44

GOTO command ??? This remember same thing:

<<
Edsger Wybe Dijkstra (Rotterdam, May 11, 1930 - Nuenen, August 6, 2002) was
a Dutch computer scientist. He received the 1972 A. M. Turing Award for
fundamental contributions in the area of programming languages.
He was also known for his low opinion of the GOTO statement in computer
programming, culminating in the 1968 article "Go To Statement Considered
Harmful" (EWD215) [1], regarded as a major step towards the widespread
deprecation of the GOTO statement and its effective replacement by control
structures such as the while loop. The paper's famous title was not the work
of Dijkstra, but of Niklaus Wirth, then editor of Communications of the ACM.
Dijkstra was known to be a fan of ALGOL 60, and worked on the team that
implemented the first compiler for that language. Dijkstra and Jaap
Zonneveld, who collaborated on the compiler, agreed not to shave until the
project was completed. Zonneveld eventually shaved off his beard; Dijkstra
kept his until his death."

Writings by E.W. Dijkstra
Go To Statement Considered Harmful, Communications of the ACM, Vol. 11
(1968) 147-148; online edition (EWD215)
How do we tell truths that might hurt? (EWD498)
From My Life (EWD166)

from http://en.wikipedia.org/wiki/Edsger_Dijkstra >>


"Josh Hoppenjans" <jh333@evansville.edu> wrote in
news:ef2621c.-1@webx.raydaftYaTP...
> i was wondering if there was anything like a goto command.
> i am trying to go from the end of and if statement back to the
> beginning of the program.

Subject: a goto command???

From: noob

Date: 20 Jan, 2006 15:49:57

Message: 8 of 44

goto isn't scientifically or intellectually a good command; you can
rewrite your code more logically. in official code it is a no-no as
John suggested.

but i have to admit, when you need to whip something up in 10 seconds
and you just impatiently want to see the result of some segment of
code without using your brain or dealing with some overhead stuff,
it's nice to have now and then.
-noob

Subject: a goto command???

From: CRet

Date: 20 Jan, 2006 22:26:44

Message: 9 of 44

I am not agree with you Noob,we have the ability to Comment and Uncomment
the selection in the editor to squizz in 10s code segments.
(REF "Noob" <Noob@Noob.Noob> wrote in news:ef2621c.7@webx.raydaftYaTP...).
Fortunately, we have no more to search for hours were to point with a GOTO
stament. I remenber the days lost searching for missfit GOTO or renumberring
old BASIC codes !!! Imaging a GOTO stament pointing in the ELSE section of
a stament or in the middle of a complex list of SET/GET properties !!! It
simply will be a mess (like it was often at programming prehistory).
How lucking are the ones who never have know code containing GOTO statments
!
And of course the maintenance of it such as John D'Errico point it out !

"John D'Errico" <woodchips@rochester.rr.com> point this out in
news:ef2621c.5@webx.raydaftYaTP...
>
> Spend a few hours/dats/weeks/months trying to
> debug spaghetti code created by liberal use of
> goto statements and see if you still think its
> easier.
>
As John D'E, I will also dispute about the fact that GOTO stament is
obsolete !

With best regards.
C.RET

P.S.: But of course, I wan't offence you Noob of any kid. I am only not
agree with your point of view.

Subject: a goto command???

From: John D'Errico

Date: 20 Jan, 2006 16:40:53

Message: 10 of 44

Noob wrote:
>
>
> goto isn't scientifically or intellectually a good command; you can
> rewrite your code more logically. in official code it is a no-no as
> John suggested.
>
> but i have to admit, when you need to whip something up in 10
> seconds
> and you just impatiently want to see the result of some segment of
> code without using your brain or dealing with some overhead stuff,
> it's nice to have now and then.
> -noob

Years ago I worked with a fellow who liked to use
goto. He had a deck of cards (fortran) that was
the "mother" of all programs. When he wanted to
run another program, rather than duplicating the
JCL cards in front of the deck, he added a goto
at the beginning of this "mother" deck, bouncing
around to the new code. Even if all the new code
did was to balance his checking account, he read
in the entire deck, compiled the entire mess for
one little routine.

Did he remove the latest goto before adding
another? No. Just another branch around the last
mess.

God save us from the goto.

John

Subject: a goto command???

From: Mr SoleCentral

Date: 20 Jan, 2006 23:56:38

Message: 11 of 44

berniecr@clarkson.edu wrote:
> a goto command can help you skip code. It could, in some circumstances,
> make programming easier.
>

Post any bit of code with a goto in it and somebdy here will be able to
show you the same functionality written without it.

However, I believe that in generated Stateflow code there are some
statechart semantics that are difficult to generate efficiently without
the use of the goto statement so you may find it in the generated
code. I have heard of this happening ( and people complaining about
it ) If this is just FUD then somebody at TMW please correct!



--
Mr SoleCentral
Cheap Birkenstock Shoes & Sandles in Australia
http://www.solecentral.com.au

Subject: a goto command???

From: Otog

Date: 20 Jan, 2006 19:37:12

Message: 12 of 44

Noob wrote:
>
>
> goto isn't scientifically or intellectually a good command; you can
> rewrite your code more logically. in official code it is a no-no as
> John suggested.
>
> but i have to admit, when you need to whip something up in 10
> seconds
> and you just impatiently want to see the result of some segment of
> code without using your brain or dealing with some overhead stuff,
> it's nice to have now and then.
> -noob
I don't see how the use of goto allows faster coding, unless you
learned spaghetti coding in the first place. However, goto has a
legitimate use (in "official" code, even), which is to do a forward
exit from a loop. Most modern languages contain a version of goto
(or a labelled 'continue' or 'break'); Matlab itself allows both
continue and break, but without the label. It is of course possible
to write code without it, it just means you need to make copies of
blocks of code instead of a single goto statement. That's why lots
of autogenerators use goto; it results in more efficient code if used
properly.
Dijkstra wrote in 1968, back when "if-then" was a novelty. He
correctly noted that control structures like "while" and "for" should
replace "goto" for coding clarity. His article did not address
forward exits, probably because it is a minor detail compared to the
larger concept of structured programming.

The argument against "goto" usually devolves into a religious battle.
 Those who don't understand its proper use rely instead on deeply
rooted faith that "goto harmful". It is not possible to convert true
believers with mere facts. (But it is fun to rattle their cages.)

Subject: a goto command???

From: berniecr@clarkson.edu

Date: 21 Jan, 2006 16:33:48

Message: 13 of 44

It could make code look nicer in some cases. I like my code nice and
neat. Because of that, i end up writing tons of little functions, and
keep nesting them. However that somerimes complicates things be
needing to pass variables back and forth. It'd be nice to read the top
layer of code and understand what is going on without having to
actually read all the code. Dig as deep as you need to.

Certainly extensive use of GOTO needs to be avoided. That doesn't mean
it should never be allowed. We aren't all developing commerical
applications, or even programs that anyone else would ever see.
Nothing wrong with GOTO for "first drafts" either. Fixing up code is a
good way to pass time when waiting for something else, or for when you
feel like a break from your main task.

I don't rember what i wanted to use GOTO for anymore. It probably was
to get out of a loop under special circumstances or something. Or
maybe it was to reuse already written code. The less lines of code,
the neater it looks.

I am not a programmer. I started writing simple programs for fun. Now
i have to write matlab programs for my project, because its much
cheaper and more flexible than any commercial software that might
already exist. If i come up with anything that works really well,
we'll need a CS student to completley rewrite it anyways, in something
like C.

In conclusion, yes GOTO should be discouraged, but it should not be
prevented. I hate computers telling me what i can and can not do.

Subject: a goto command???

From: Doug Schwarz

Date: 22 Jan, 2006 02:22:49

Message: 14 of 44

In article <1137890028.184278.60710@z14g2000cwz.googlegroups.com>,
 "berniecr@clarkson.edu" <berniecr@clarkson.edu> wrote:

> In conclusion, yes GOTO should be discouraged, but it should not be
> prevented. I hate computers telling me what i can and can not do.

What you seem to be missing is that while you might use goto
responsibly, others will not. For those of us who support other
people's code goto is a nightmare. I am glad that MATLAB doesn't have
it.

The computer is not telling you what you can and cannot do. You chose
to use a language without a goto statement. Feel free to change your
mind.

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.

Subject: a goto command???

From: Steve Amphlett

Date: 22 Jan, 2006 14:34:18

Message: 15 of 44

berniecr@clarkson.edu wrote:
>
>
> I hate computers telling me what i can and can not do.

Misguided fool. It's not the "computers telling me what i [sic] can
and can not do", it's decades of programmers and compiler writers
sharing their experiences with you. "goto" (as a high level language
construct) is bad. We've all learned.

Subject: a goto command???

From: crezejames

Date: 22 Jan, 2006 16:15:30

Message: 16 of 44

Steve Amphlett wrote:
>
>
> berniecr@clarkson.edu wrote:
>>
>>
>> a goto command can help you skip code. It could, in some
>> circumstances,
>> make programming easier.
>
> goto: "somwhere I cannot come back from"

i could go back with another goto. goto is a parity problem.
-creze james!

Subject: a goto command???

From: Brad Phelan

Date: 23 Jan, 2006 12:07:08

Message: 17 of 44

> if used
> with much parsimony, can help with code writing in a
> CLEANER way.

I would be very much interested in seeing an example here of what you
think would be CLEANER. Some concrete examples would liven up the
discussion.

However if you want some trully bizare C code you can do no better
than Duff's Device. The below is perfectly valid C-Code. Can you
figure out what it does?

int n = (count + 7) / 8; /* count > 0 assumed */
  
 switch (count % 8)
 {
   case 0: do { *to++ = *from++;
   case 7: *to++ = *from++;
   case 6: *to++ = *from++;
   case 5: *to++ = *from++;
   case 4: *to++ = *from++;
   case 3: *to++ = *from++;
   case 2: *to++ = *from++;
   case 1: *to++ = *from++;
                     } while (--n > 0);
 }

--
Brad Phelan
 <http://xtargets.com>

--
Brad Phelan
 <http://xtargets.com/cms/Tutorials/Matlab-Programming/MUnit-Matlab-Unit-Testing.html>

Subject: a goto command???

From: Steve Amphlett

Date: 23 Jan, 2006 12:24:34

Message: 18 of 44

Brad Phelan wrote:
>
>
>> if used
>> with much parsimony, can help with code writing in a
>> CLEANER way.
>
> I would be very much interested in seeing an example here of what
> you
> think would be CLEANER. Some concrete examples would liven up the
> discussion.

[OT now] The only gotos or equivalent in my code (albeit C, not ML)
are the places where only siglongjmp() will do. And they are in
signal handlers, which are sort of the opposite of a goto. Really
really hard to debug, but absolutely necessary when using blocking
functions that can get stuck. If something really nasty happens
inside a function to lock a program, a user CTRL-C will jump you into
the signal handler (who doesn't know where you came from), which will
then be programmed to siglongjmp() you back to a marker you set just
before the bad function. Then you don't call it (because now you
have 20/20 hindsight), you call an error function instead.

Subject: a goto command???

From: Dave Robinson

Date: 23 Jan, 2006 12:43:43

Message: 19 of 44

Steve Amphlett wrote:
>
>
> Brad Phelan wrote:
>>
>>
>>> if used
>>> with much parsimony, can help with code writing in a
>>> CLEANER way.
>>
>> I would be very much interested in seeing an example here of
what
>> you
>> think would be CLEANER. Some concrete examples would liven up
the
>> discussion.
>
> [OT now] The only gotos or equivalent in my code (albeit C, not ML)
> are the places where only siglongjmp() will do. And they are in
> signal handlers, which are sort of the opposite of a goto. Really
> really hard to debug, but absolutely necessary when using blocking
> functions that can get stuck. If something really nasty happens
> inside a function to lock a program, a user CTRL-C will jump you
> into
> the signal handler (who doesn't know where you came from), which
> will
> then be programmed to siglongjmp() you back to a marker you set
> just
> before the bad function. Then you don't call it (because now you
> have 20/20 hindsight), you call an error function instead.

Well what you actually need is not a GoTo function, which has been
said by previous contributors leads to spagetti programming but the
ComeFrom function. Ideal for making crash proof software. Only
trouble is that it becomes difficult to follow other peoples software
as they probably wrote it backwards;-)

Subject: a goto command???

From: Rui Nabeiro

Date: 23 Jan, 2006 14:36:34

Message: 20 of 44

Brad Phelan wrote:
>
>
>> if used
>> with much parsimony, can help with code writing in a
>> CLEANER way.
>
> I would be very much interested in seeing an example here of what
> you
> think would be CLEANER. Some concrete examples would liven up the
> discussion.
>

OK, what I had in mind were situations like this

for i=...
    for j=...
       if condition
          % I want to get out of here but a can't because break only
breaks the inner loop
          % I wish I had a goto
       end
    end
end

One way of writting that is

getout = 0;
for i=...
    for j=...
       if condition
          getout = 1;
          break
       end
    end
    if getout
       break
    end
end

Well, now imagine that we had more loops. Which one would be cleaner?

Subject: a goto command???

From: Christopher Hulbert

Date: 23 Jan, 2006 14:33:56

Message: 21 of 44

Rui Nabeiro wrote:
> Brad Phelan wrote:
>
>>
>>>if used
>>>with much parsimony, can help with code writing in a
>>>CLEANER way.
>>
>>I would be very much interested in seeing an example here of what
>>you
>>think would be CLEANER. Some concrete examples would liven up the
>>discussion.
>>
>
>
> OK, what I had in mind were situations like this
>
> for i=...
> for j=...
> if condition
> % I want to get out of here but a can't because break only
> breaks the inner loop
> % I wish I had a goto
> end
> end
> end
>
> One way of writting that is
>
> getout = 0;
> for i=...
> for j=...
> if condition
> getout = 1;
> break
> end
> end
> if getout
> break
> end
> end
>
> Well, now imagine that we had more loops. Which one would be cleaner?


I think a nested function would come in handy here.

function main
...
err=loops;
% Check how we exited from loops
...

function err=loops
 > for i=...
 > for j=...
 > if condition
 > % I want to get out of here but a can't because break only
 > breaks the inner loop
 > % I wish I had a goto
             err = SOME_ERROR_CODE;
             return;
 > end
 > end
 > end
end %loops
end % main


OR perhaps we can use a try catch?

function blah
...
try
 > for i=...
 > for j=...
 > if condition
 > % I want to get out of here but a can't because break only
 > breaks the inner loop
 > % I wish I had a goto
             error('condition');
 > end
 > end
 > end
catch
% When we throw error we will get to here :)! and we don't necessarily have to
do anything in the catch.
end


Hope these suggestions help you out!

Subject: a goto command???

From: CRet

Date: 24 Jan, 2006 00:42:26

Message: 22 of 44

In same extent, BREAK or RETURN staments in FOR/WHILE loops or functions is
really clause to a kid of GOTO statment.
So perhaps, someone have already implement a GO TO statment in MatLab.

We may certainly not exhaustively used BREAK or RETURN ins Loops or Function
or the spaggeti code will strick back !
And clarity lost.
C.Ret

"Brad Phelan" <bradphelan@xtargets.com> wrote in
news:ef2621c.17@webx.raydaftYaTP...
> > if used
> > with much parsimony, can help with code writing in a
> > CLEANER way.
>

"Rui Nabeiro" <Rui.Nabeiro@gmail.com> wrote in
news:ef2621c.16@webx.raydaftYaTP...
> This is only an argumentative discussion because no one believes that
> TMW will ever implement a goto.
> My view is that gotos are perfectly valid instructions that, if used
> with much parsimony, can help with code writing in a CLEANER way. The
> point that long experience has shown endless examples of (very) bad
> use should not, again in my view of the thing, be dictatorial on the
> goto eradication.

Subject: a goto command???

From: Steve Amphlett

Date: 24 Jan, 2006 04:20:00

Message: 23 of 44

Rui Nabeiro wrote:
>
>
> OK, what I had in mind were situations like this
>
> for i=...
> for j=...
> if condition
> % I want to get out of here but a can't because break
> only
> breaks the inner loop
> % I wish I had a goto
> end
> end
> end
>
> One way of writting that is
>
> getout = 0;
> for i=...
> for j=...
> if condition
> getout = 1;
> break
> end
> end
> if getout
> break
> end
> end
>
> Well, now imagine that we had more loops. Which one would be
> cleaner?

Neither is cleaner. Vectorize your code instead. ML is quite good
at that, it has logical indexing to avoid all those "if" tests too.

Subject: a goto command???

From: mat

Date: 24 Jan, 2006 08:49:33

Message: 24 of 44

The story about GOTO command repeats time to time in this newsgroup.
And it is always the same: most participants in the discussion could
be classified in two clans, a GOTO clan and an ANTI-GOTO clan.
Someone said these clans are almost like a religious sects, and what
is the worst, he is right. Yet, the ANTI-GOTO clan members are much
more aggressive.
This matter about the GOTO command is somehow similar to vectorizing
in ML. Namely, vectorizing is offered in ML; if someone wants to use
it, they are welcome; on the contrary, for those who dislike
vectorizing, who don't know how to use it, or at last, for the
problems that are not able to be vectorized, still there are
"classical" ways of programming. Something similar should be made
about GOTO statement. For those who get crazy when someone mention
even "G" of "GOTO", they don't need to use it. On the other hand, for
those who got used on GOTO, let them use it as much as they need.
I also agree with someone who said that he hated any limitations
obtruded by computers.
We all work in ML quite well even without GOTO command, but if it
existed, I can bet, most of us would use it time to time.

Mat

Subject: a goto command???

From: Steve Amphlett

Date: 24 Jan, 2006 08:55:23

Message: 25 of 44

Mat wrote:
>
>
> We all work in ML quite well even without GOTO command, but if it
> existed, I can bet, most of us would use it time to time.

C has a goto command. But I have never seen it used, even by newbie
C programmers. Maybe it is redundant in that language - other
commands perform the function better (e.g. break, continue, switch)?

Subject: a goto command???

From: John D'Errico

Date: 24 Jan, 2006 14:05:20

Message: 26 of 44

In article <ef2621c.26@webx.raydaftYaTP>, Mat <mat_labor@yahoo.com> wrote:

> The story about GOTO command repeats time to time in this newsgroup.
> And it is always the same: most participants in the discussion could
> be classified in two clans, a GOTO clan and an ANTI-GOTO clan.
> Someone said these clans are almost like a religious sects, and what
> is the worst, he is right. Yet, the ANTI-GOTO clan members are much
> more aggressive.
> This matter about the GOTO command is somehow similar to vectorizing
> in ML. Namely, vectorizing is offered in ML; if someone wants to use
> it, they are welcome; on the contrary, for those who dislike
> vectorizing, who don't know how to use it, or at last, for the
> problems that are not able to be vectorized, still there are
> "classical" ways of programming. Something similar should be made
> about GOTO statement. For those who get crazy when someone mention
> even "G" of "GOTO", they don't need to use it. On the other hand, for
> those who got used on GOTO, let them use it as much as they need.
> I also agree with someone who said that he hated any limitations
> obtruded by computers.
> We all work in ML quite well even without GOTO command, but if it
> existed, I can bet, most of us would use it time to time.


The problem is not that I personally would use goto even
if it were offered.

I spent a fair amount of my career providing libraries of
code for a large group of users. I've retired now, and there
are others who must worry about code maintenance for that
body of code. I wonder what nasty things would be said about
me had I used goto liberally in my software libraries?

Your code is not always just your own code. It gets passed
on to others. And, GOD FORBID, it occasionally has a bug in
it. (Ok, your code never has a bug in it.) What happens when
a new release of Matlab comes out with a subtle change that
forces your successor to examine a piece of your legacy code?

Your successor may well just choose to re-write your code
from scratch rather than follow your tortured logic. I've
been forced to do exactly that in some cases rather than
chase through spaghetti code. I'll admit that I preceded
this enforced re-write with some choice swear words.

John


--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

Subject: a goto command???

From: Doug Schwarz

Date: 24 Jan, 2006 14:11:28

Message: 27 of 44

In article <ef2621c.26@webx.raydaftYaTP>, Mat <mat_labor@yahoo.com>
wrote:

> The story about GOTO command repeats time to time in this newsgroup.
> And it is always the same: most participants in the discussion could
> be classified in two clans, a GOTO clan and an ANTI-GOTO clan.
> Someone said these clans are almost like a religious sects, and what
> is the worst, he is right. Yet, the ANTI-GOTO clan members are much
> more aggressive.
> This matter about the GOTO command is somehow similar to vectorizing
> in ML. Namely, vectorizing is offered in ML; if someone wants to use
> it, they are welcome; on the contrary, for those who dislike
> vectorizing, who don't know how to use it, or at last, for the
> problems that are not able to be vectorized, still there are
> "classical" ways of programming. Something similar should be made
> about GOTO statement. For those who get crazy when someone mention
> even "G" of "GOTO", they don't need to use it. On the other hand, for
> those who got used on GOTO, let them use it as much as they need.
> I also agree with someone who said that he hated any limitations
> obtruded by computers.
> We all work in ML quite well even without GOTO command, but if it
> existed, I can bet, most of us would use it time to time.
>
> Mat

I completely disagree with you.

Vectorization *is the main advantage* to using MATLAB. If you don't
want to vectorize your code, use some other language. Loops are
available for those times when vectorization is not possible, but tend
to make the code harder to understand (though not always). A goto
command, used sparingly and properly, can help simplify code, but (and
here is the important point) is very easy to abuse. Many people will
abuse it and the result is code that is harder to understand.

I think you will find that those people who oppose the goto sometimes
have to read other people's code and those people who are in favor of it
don't. I am certainly capable of avoiding a language feature I don't
like, but I can't make you avoid it. That's what's wrong with goto --
it makes bad code easy to write.

This issue has nothing to do with whether a goto statement can have
advantages -- it does -- but at a huge cost.

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.

Subject: a goto command???

From: Duane Hanselman

Date: 24 Jan, 2006 09:22:49

Message: 28 of 44

Mat wrote:
>
>
>...
For those who get crazy when someone mention
> even "G" of "GOTO", they don't need to use it. On the other hand,
> for
> those who got used on GOTO, let them use it as much as they need.
...
> Mat

This philosophy works fine as long as the two clans remain isolated
from each other. I can bet that the "agressive" anti-GoTO folks would
never want to review, contribute, or otherwise mess with code
containing goto statements. Perhaps the pro-GOTO folks would not like
to review, contribute, or otherwise mess with code that doesn't
contain goto statements. Or maybe the pro-GOTO folks are too
"anti-agressive" compared to those "agressive" anti-GOTO folks?

Subject: a goto command???

From: Scott Seidman

Date: 24 Jan, 2006 14:17:38

Message: 29 of 44

John D'Errico <woodchips@rochester.rr.com> wrote in news:woodchips-
3E186B.09052024012006@syrcnyrdrs-01-ge0.nyroc.rr.com:

> Your code is not always just your own code. It gets passed
> on to others. And, GOD FORBID, it occasionally has a bug in
> it. (Ok, your code never has a bug in it.) What happens when
> a new release of Matlab comes out with a subtle change that
> forces your successor to examine a piece of your legacy code?

I've always been told to code as if there will be two people trying to
decipher it. Me, and Me six weeks after the fact!

--
Scott
Reverse name to reply

Subject: a goto command???

From: Peter Boettcher

Date: 24 Jan, 2006 09:48:49

Message: 30 of 44

"Steve Amphlett" <Firstname.Lastname@where_I_work.com> writes:

> Mat wrote:
>>
>>
>> We all work in ML quite well even without GOTO command, but if it
>> existed, I can bet, most of us would use it time to time.
>
> C has a goto command. But I have never seen it used, even by newbie
> C programmers. Maybe it is redundant in that language - other
> commands perform the function better (e.g. break, continue, switch)?

I use it occassionally, usually for error handling. One case: inside
2 'for' loops, there are 2 more nested while loops. On an overflow
condition inside the inner one, I want to leave both while loops, skip
the next several commands, and continue with the next outer loop.
Sure, that could be done with a break, another condition test with
another break in the next loop, then an if statement on the same
condition on the statements to skip. But in this case, it is far
cleaner to write:

goto escape; // Error, skip out of these loops, cleanup, and start
              // the next block

Cleaner, more readable, better performance, no extra flag variables
that take up registers, etc.


I treat goto like I treat eval in MATLAB. When beginners want to use
eval, there is almost always a better way to do it. But that doesn't
mean that eval is never the best way to write something.

Rgrepping through the full source tree of the Apache web server, for
instance, turns up 259 gotos. Almost all of them are 'goto unlock',
'goto error', 'goto done', 'goto start_over', etc.


--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: a goto command???

From: Peter Boettcher

Date: 24 Jan, 2006 09:53:06

Message: 31 of 44

Doug Schwarz <see@sig.for.address.edu> writes:

[snip]

> A goto command, used sparingly and properly, can help simplify
> code, but (and here is the important point) is very easy to abuse.
> Many people will abuse it and the result is code that is harder to
> understand.
>
> I think you will find that those people who oppose the goto sometimes
> have to read other people's code and those people who are in favor of it
> don't. I am certainly capable of avoiding a language feature I don't
> like, but I can't make you avoid it. That's what's wrong with goto --
> it makes bad code easy to write.

Hmm. I guess I'm in favor of clean code, whatever the tools used to
get there. Programmers abuse eval, but that doesn't mean the right
solution is to eliminate it. Programmers do horrible things with
global variables, but sometimes it's the cleanest way.

I'm not really arguing for introducing 'goto' in MATLAB, BTW, I just
disagree with the crowd that always evil.


--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: a goto command???

From: Doug Schwarz

Date: 24 Jan, 2006 15:07:04

Message: 32 of 44

In article <m34q3t4qzx.fsf@coyote.llan.ll.mit.edu>,
 Peter Boettcher <boettcher@ll.mit.edu> wrote:

> Doug Schwarz <see@sig.for.address.edu> writes:
>
> [snip]
>
> > A goto command, used sparingly and properly, can help simplify
> > code, but (and here is the important point) is very easy to abuse.
> > Many people will abuse it and the result is code that is harder to
> > understand.
> >
> > I think you will find that those people who oppose the goto sometimes
> > have to read other people's code and those people who are in favor of it
> > don't. I am certainly capable of avoiding a language feature I don't
> > like, but I can't make you avoid it. That's what's wrong with goto --
> > it makes bad code easy to write.
>
> Hmm. I guess I'm in favor of clean code, whatever the tools used to
> get there. Programmers abuse eval, but that doesn't mean the right
> solution is to eliminate it. Programmers do horrible things with
> global variables, but sometimes it's the cleanest way.
>
> I'm not really arguing for introducing 'goto' in MATLAB, BTW, I just
> disagree with the crowd that always evil.

Peter,

You snipped my last sentence:

"This issue has nothing to do with whether a goto statement can have
advantages -- it does -- but at a huge cost."

I hope it's clear that I agree with you that goto is not always evil.

The difference between goto and eval is there are *always* code
workarounds for goto, but not for eval -- usually, but not always.

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.

Subject: a goto command???

From: Paul Skoczylas

Date: 24 Jan, 2006 15:15:30

Message: 33 of 44

"Mat" <mat_labor@yahoo.com> wrote:
> We all work in ML quite well even without GOTO command, but if it
> existed, I can bet, most of us would use it time to time.

I can't think of a time when I wanted a goto in Matlab. But I won't swear
that I would never use it. I do a fair bit of VB programming (mostly VBA
within Excel), and I think I've used a goto maybe two or three times there,
ever. It's not a NEVER situation, but as close as it gets. For Matlab,
it's not there, and I don't miss it!

My first programming was in the old Basic as a kid. There were no while
loops. We did have For/Next, but goto was a way of life. Then in grade 10,
I took Pascal. I don't know if Pascal has a goto statement or not (not
having used it since grade 10), but if it did, we weren't allowed to use it
in that class. I distinctly remember one assignment which was to make a
Monopoly game. At the time I thought it would be absolutely impossible to
write such a program without any gotos, but I did it, and it wasn't hard!

-Paul

Subject: a goto command???

From: Peter Boettcher

Date: 24 Jan, 2006 10:57:31

Message: 34 of 44

Doug Schwarz <see@sig.for.address.edu> writes:

> In article <m34q3t4qzx.fsf@coyote.llan.ll.mit.edu>,
> Peter Boettcher <boettcher@ll.mit.edu> wrote:
>
>> Hmm. I guess I'm in favor of clean code, whatever the tools used to
>> get there. Programmers abuse eval, but that doesn't mean the right
>> solution is to eliminate it. Programmers do horrible things with
>> global variables, but sometimes it's the cleanest way.
>>
>> I'm not really arguing for introducing 'goto' in MATLAB, BTW, I just
>> disagree with the crowd that always evil.
>
> Peter,
>
> You snipped my last sentence:
>
> "This issue has nothing to do with whether a goto statement can have
> advantages -- it does -- but at a huge cost."
>
> I hope it's clear that I agree with you that goto is not always evil.
>
> The difference between goto and eval is there are *always* code
> workarounds for goto, but not for eval -- usually, but not always.

Sorry about that snip. It was clear from your post that you're not in
the goto-is-evil crowd. We're debating the finer points of "Goto is
easily abuseable, but sometimes useful. What do we do about it?"



--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: a goto command???

From: Michael Wild

Date: 24 Jan, 2006 17:30:26

Message: 35 of 44

Paul Skoczylas wrote:
> "Mat" <mat_labor@yahoo.com> wrote:
>
>>We all work in ML quite well even without GOTO command, but if it
>>existed, I can bet, most of us would use it time to time.
>
>
> I can't think of a time when I wanted a goto in Matlab. But I won't swear
> that I would never use it. I do a fair bit of VB programming (mostly VBA
> within Excel), and I think I've used a goto maybe two or three times there,
> ever. It's not a NEVER situation, but as close as it gets. For Matlab,
> it's not there, and I don't miss it!
>
> My first programming was in the old Basic as a kid. There were no while
> loops. We did have For/Next, but goto was a way of life. Then in grade 10,
> I took Pascal. I don't know if Pascal has a goto statement or not (not
> having used it since grade 10), but if it did, we weren't allowed to use it
> in that class. I distinctly remember one assignment which was to make a
> Monopoly game. At the time I thought it would be absolutely impossible to
> write such a program without any gotos, but I did it, and it wasn't hard!
>
> -Paul
>
>

just as a remark: pascal (or at least ObjectPascal, which I know) has one.

I think Fortran 90 introduced a nice feature for those missing goto to
get out of loops: you can give names to any flow control statements. for
example you can label the outer loop with 'outer' and then do a "exit
outer" in the inner loop to leave both.
Perhaps other language-'commitees' should think about introducing
similar features?

Michael

Subject: a goto command???

From: Doug Schwarz

Date: 24 Jan, 2006 16:31:06

Message: 36 of 44

In article <m3zmll39g4.fsf@coyote.llan.ll.mit.edu>,
 Peter Boettcher <boettcher@ll.mit.edu> wrote:

> Doug Schwarz <see@sig.for.address.edu> writes:
>
> > I hope it's clear that I agree with you that goto is not always evil.


> Sorry about that snip. It was clear from your post that you're not in
> the goto-is-evil crowd. We're debating the finer points of "Goto is
> easily abuseable, but sometimes useful. What do we do about it?"

I'm just in the goto-is-sinful crowd. :-) (To sin, in this case, means
"to miss the mark".)

Well, *we* can't do anything about it, only TMW can. I'm content to let
it drop. I think it would take a strong argument indeed to get TMW to
add goto to the language. Only weak arguments, at best, have been put
forth up to now.

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.

Subject: a goto command???

From: Brad Phelan

Date: 24 Jan, 2006 12:12:58

Message: 37 of 44


>> char x[] = "abcdefg";
>> int i=0;
>> char sep = ','
>>
>> goto enter:
>> do{
>> printf(sep);
>
> printf takes a const char * as its first argument and you just passed it a
> character.

Oops forgive my sloppy brain compiler !

>> This saves a comparison operation on each loop iteration. You can do the
>> above without goto using switch just like in Duff's device but it is
>> arguable as to whether it is clearer than using goto.
>>
>> void main(void){
>>
>> char x[] = "abcdefg";
>> int i=0;
>>
>> switch(1){
>> do{
>> printf(",");
>> case 1:
>> printf("%c",x[i]);
>> }while(i++<6);
>> }
>> printf("\n");
>> }
>
> I think just printing the first value before the loop is a better choice.

Of course I didn't want to give an example that was too complicated. I
was hoping that you could extend the idea in your head. The code of
each loop component could be arbitrarily long. Joining a list of
chars with a separator is a simple example. How about cumulative
summing a list of integers whilst performing an operation on each
item.

If you replicate the code outside the loop you duplicate code and
thus have a higher risk of error.

void main(void){

      int x[] = {1, 3, 6, ...... Nth };
      int i=0;

      switch(1){
            do{
               x[i]=x[i-1]+x[i];
         case 1:
               /* Some complicated
                  bit of code that we
                  don't want to put into
                  a function.
               */
               x[i] = result_of_above_code;
            }while(i++<N);
      }
      printf("\n");
  }

BTW. I'm not advocating goto. I am in the goto is evil camp. However
there are some constructs that do not appear in normal languages that
do have a use case and should be fixed by adding proper language
features to handle them.

I think the switch trick above is ok because it locally bounds
the label. You can't do an arbitrary jump into the block from
anywhere. Thus it is more restrictive than goto and safer. Still,
the first time I saw the above pattern I thought it very ugly
but now it grows on me :)

B

>
>>
>>
>> --
>> Brad Phelan
>> Integrate Legacy Code With RTW Easy - Free Download
>> http://xtargets.com/cms/Tutorials/Matlab-Programming/Simulink-RTW-Legacy-Code-Device-Driver-Tool.html
>>
>>

Subject: a goto command???

From: GOTO dog

Date: 25 Jan, 2006 18:26:12

Message: 38 of 44

Ken Garrard wrote:
 
> It took about 10 weeks to debug the "working" code. There were only
> a
> half dozen errors all dealing with things like un-initialized
> variables, not testing for zero before a divide, system i/o unit
> numbers and the damn GOTOs jumping into the middle of a loop from
> the
> middle of another loop. The GOTO part of the job consumed 9 weeks
> and
> 3 days of the total. The final report detailing the "errors" and
> the
> corrections was only two pages.

> There hasn't been a GOTO in any non-assembler code that I've
> written
> and there never will be. In my opinion GOTOs can always be
> elegantly
> and efficiently avoided. And doing so always saves time and
> clarifies
> the problem being solved.
>
> Ken Garrard
> North Carolina State University

Our neighbors had a dog called GOTO. The damn beast barked the entire
night and it didn't let me sleep. Since then when I see a GOTO I
don't sleep for a week.

Subject: a goto command???

From: Cy

Date: 25 Jan, 2006 22:55:05

Message: 39 of 44

Can I ask a simple question here?
Why exactly GOTO is the cause of spaghetti code?
Is it because in case of bug one wouldn't know from where the code
branched from to where it crashed?

cY

Subject: a goto command???

From: Ionica Smeets

Date: 26 Jan, 2006 06:34:40

Message: 40 of 44

...Post any bit of code with a goto in it and somebody here will be
able to show you the same functionality written without it...

Ok, I would like that! I am implementing the Lenstra-Lenstra-Lovasz
algorithm in Matlab and in the original article the pseudo-code
roughly looks like this:

***
compute gram-schmidt constants for a given basis b;
k = 2;

(1) do a subroutine;

if(condition) goto (2);

do a subroutine;

if(k=n) terminate;
k = k+1;
goto (1)

(2) do some calculations
k = k-1;
goto (1)
***

So, how should I work around the goto's here?

Subject: a goto command???

From: John D'Errico

Date: 27 Jan, 2006 15:35:05

Message: 41 of 44

In article <ef2621c.52@webx.raydaftYaTP>, cY <49427@cantv.net> wrote:

> Can I ask a simple question here?
> Why exactly GOTO is the cause of spaghetti code?
> Is it because in case of bug one wouldn't know from where the code
> branched from to where it crashed?


The goto is not inherently evil. Its the spaghetti coders
who are themselves "evil". They use a tool that has value
to accomplish "evil" acts. Goto is an enabler of evil.

I can't ask to remove goto from languages that have it
already. But I can hope that it is never implemented in
Matlab. The cost of goto when placed in the wrong hands
is far higher than the minor gain to be seen by worthy
hands.

John


--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

Subject: a goto command???

From: Michael Wild

Date: 30 Jan, 2006 22:34:38

Message: 42 of 44

John E M Brown wrote:
> Dear programming geniuses!
> Like Ionica Smeets - I need some help. I wish I had found this discussion just a couple of days earlier!! Here is my problem. I am translating a fortran subroutine to MATLAB. It has a couple of GOTO's in it. How do I work around the GOTO statements? John
>
> Associated Comments to Subroutine
> !check if the current ii,jj cell routes down
> !to the subbasin outlet point, following the
> !direction of direc(,) from each cell
> !if you get there, no_of_box increments
> !and you try another cell.
> !N,M - station location
>
> NO_OF_BOX = 0
> DO I = 1, ICOL
> DO J = 1, IROW
> II = I
> JJ = J
> 300 CONTINUE
> IF ((II .GT. ICOL) .OR. (II .LT.1) .OR.
> & (JJ .GT. IROW) .OR. (JJ .LT.1)) THEN
> GOTO 310
> END IF
> IF ((II .EQ. N) .AND. (JJ .EQ. M)) THEN
> NO_OF_BOX = NO_OF_BOX + 1
> CATCHIJ(NO_OF_BOX,1) = I
> CATCHIJ(NO_OF_BOX,2) = J
> GOTO 310
> ELSE
> IF ((DIREC(II,JJ,1).NE.0) .AND.
> & (DIREC(II,JJ,2) .NE.0)) THEN
> III = DIREC(II,JJ,1)
> JJJ = DIREC(II,JJ,2)
> II = III
> JJ = JJJ
> GOTO 300
> END IF
> END IF
> 310 CONTINUE
> END DO
> END DO
>
> WRITE(*,*) 'Number of grid cells upstream of present station',
> $ no_of_box
>
> RETURN
> END


replace the "goto 310" with "cycle" or "continue" (fortran vs. matlab).
The "goto 300" is tougher. I propose an indicator boolean/logical which
you set initially to false and test for what assignment you need to make
  just before the "300 continue" and move the two assignments before the
goto 300 up.

no_of_box = 0;
test = false;
iii = 0;
jjj = 0;
for i=1:icol
   for j=1:irow
     if test
       ii = iii;
       jj = jjj;
       test = false;
     else
       ii = i;
       jj = j;
     end

     if (ii > icol) || (ii < 1) ||(jj > irow) || (jj < 1)
       continue;
     end
     if (ii == N) && (jj == M)
       no_of_box = no_of_box + 1;
       catchij(no_of_box,1) = i;
       catchij(no_of_box,2) = j;
       continue;
     elseif (direc(ii,jj,1)~=0) && (direc,ii,jj,2) ~= 0)
       iii = direc(ii,jj,1);
       jjj = direc(ii,jj,2);
       test = true;
     end
   end
end



michael

Subject: a goto command???

From: Michael Wild

Date: 31 Jan, 2006 00:21:03

Message: 43 of 44

John E M Brown wrote:
> Michael,
>
> Wow, I will give it a try ASAP!!
>
> Thanks for the help. I had a hard time getting the essence of the tests.
>
> John

hope I got them right :-) i'm sure with some more thought one could
simplify the whole thing...

michael

Subject: a goto command???

From: saleem

Date: 11 Feb, 2006 09:37:47

Message: 44 of 44

berniecr@clarkson.edu wrote:
>
>
> a goto command can help you skip code. It could, in some
> circumstances,
> make programming easier.
>
>

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com