Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » What about goto?

This thread is locked; no one can reply to it. rss feed Print
 1   2 
What about goto?
Albin Engström
Member #8,110
December 2006
avatar

I have a clouded memory of people saying "goto shouldn't be used", but i can think of alot of places when i want to use it, so please tell me what you think||know about "goto".

Matthew Leverton
Supreme Loser
January 1999
avatar

Thomas Fjellstrom
Member #476
June 2000
avatar

goto can be very evil.

However it does have a few good uses. I can think of two right off the bat:

  • Error handling in large C functions.

  • Computed "goto" dispatch in a Virtual Machine's core.

edit: See the link in matthew's post ;)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro SVN Snapshots] - [Allegro TODO] - [Web Hosting]
"God Bless Joe Pesci" -- George Carlin
"Goto is the buldozer of coding. Sometimes, the buldozer is just the right tool for the job. Not often, but sometimes." -- LordBob

Paul whoknows
Member #5,081
September 2004
avatar

Quote:

so please tell me what you think||know about "goto".

You don't need it, if you know how to use these instructions properly: do-while, switch, if-else.
Goto is ok for Basic programmers, but in C/C++ you have better and more elegant alternatives.
I have never used a goto in my C/C++ projects, and I will never do it.

Albin Engström
Member #8,110
December 2006
avatar

Understood, thank you :)

torhu
Member #2,727
September 2002
avatar

Use goto when it makes your code clearer. It's often used where you would use catch and finally blocks in java, ie. for jumping to some error cleanup code at the end of a function. And sometimes it's the cleanest way of breaking out of nested loops. But if you're using it to jump backwards, you better make sure there really isn't a better way. I've seen some messy code doing backwards jumps.

Bruce Perry
Member #270
April 2000

Quote:

Goto is ok for Basic programmers, but in C/C++ you have better and more elegant alternatives.

Are there really versions of BASIC that don't have proper looping constructs? BBC BASIC has REPEAT...UNTIL, FOR...NEXT and IF...ELSE. (OK, come to think of it, it doesn't have multi-line IF. Hmm.) There's no SWITCH but you can do that using IFs (and C's 'switch' is little more than gotos anyway, considering the way it falls through).

Incidentally, Java has a very good answer to 'goto'. It lets you label any statement (including loop statements that contain lots of nested statements), and then any nested statement can be a 'break' with that label - or a 'continue' if the labelled statement is a loop. It means you can break out of arbitrarily nested loops and jump to the next statement afterwards, and it keeps your code well-structured. You do indeed end up with no way to jump back without using a loop.

In C and C++, I'd have no problem with using a 'goto' to simulate that.

I have also used 'goto' to jump back though. I did that when the code wouldn't normally loop but there was just one specific case where I wanted it to repeat the body of the function. Sometimes it's a bit counter-intuitive to see a 'while' loop when it's not obvious why the code is going to loop. It's arguable though.

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
The brxybrytl has you.

nonnus29
Member #2,606
August 2002
avatar

BP's right; the break and continue keywords are actually controlled goto's.

I think historically goto got a bad rap from back in the fortran/cobol days when that was all they had; Fortran didn't have if/then/else for a LONG time. Cobols paragraph structure is a sort of controlled procedure branching construct that goto would desrupt. If you goto'd/jumped out of a paragraph, the pgm would execute straight thru to the end. Then in the early MS basics like that on the c-64 all you had was goto and gosub.

In the 60's some guys proved that any flowchart could be implemented using only if/then/else and a pre-test loop like while(){}. That's one thing that led to the 'structured programming' revolution. It's hard to believe that something we take for granted and seems so obvious was revolutionary at one time.

Bruce Perry
Member #270
April 2000

Quote:

Then in the early MS basics like that on the c-64 all you had was goto and gosub.

Lame! The BBC Micro predated the C64, by about four years I believe.

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
The brxybrytl has you.

ReyBrujo
Moderator
January 2001
avatar

Goto is the way you handle exceptions in Visual Basic.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Thomas Fjellstrom
Member #476
June 2000
avatar

Perl has some interesting semantics with labels and goto...

heres a nice quote from the man himself:

Larry Wall said:

If I allowed "next $label" then I'd also have to allow "goto $label", and I don't think you really want that... :-)

you can label a loop, and "next/last" on them, as if they were named loops. its rather handy.

FOO:
while(...) {
   BAR: for(...) { ... next FOO if $blah; next BAR if $baz; ... }
}

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro SVN Snapshots] - [Allegro TODO] - [Web Hosting]
"God Bless Joe Pesci" -- George Carlin
"Goto is the buldozer of coding. Sometimes, the buldozer is just the right tool for the job. Not often, but sometimes." -- LordBob

Bob
Free Market Evangelist
September 2000
avatar

Goto is the buldozer of coding. Sometimes, the buldozer is just the right tool for the job. Not often, but sometimes.

--
- Bob
[ Webpage | Allegro FAQ | Coding Tricks ]
"Oh, you want to do actual work. In that case, avoid the GameCube at all costs!" - Me

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Goto is the buldozer of coding. Sometimes, the buldozer is just the right tool for the job. Not often, but sometimes.

Sigged for posterity.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro SVN Snapshots] - [Allegro TODO] - [Web Hosting]
"God Bless Joe Pesci" -- George Carlin
"Goto is the buldozer of coding. Sometimes, the buldozer is just the right tool for the job. Not often, but sometimes." -- LordBob

Johan Halmén
Member #1,550
September 2001

"Why does a dog lick its penis?"
"Because it can."

I think goto is the most strange thing in C/C++. We all know it belongs to BASIC. We also know that it is a part of the standard C/C++. But I bet it is the only thing coders find strange, something that doesn't match the way of thinking and way of using the language.

I speak a kind of old Swedish (because I live in Finland), which is not influenced by English in same extent than the Swedish spoken in Sweden. It strikes me every time I see or hear a new English word in Swedish printed or spoken in Sweden, used as if it were a part of the language. But not only words. Some structures in sentences are also adapted from English. I find it horrible. A bit like using goto in C/C++.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This thread is obviously about nothing so heres a bunny with a pancake on its head. -kazzmir

orz
Member #565
August 2000

Quote:

But I bet it is the only thing coders find strange, something that doesn't match the way of thinking and way of using the language.

What about trigraphs? And macros. And the way ">>" is treated when terminating nested template parameters. And the way variable initializations are sometimes treated as function declarations. And...

Well, really, I agree with the point that goto is conceptually closer kin to asm or basic and is out of place in C and even farther from the intended paradigm of C++. I'm just saying that there's a bunch of other very strange stuff that somehow made it in to C++ too.

Johan Halmén
Member #1,550
September 2001

Ok, you might be right, there are more oddities than goto. I never knew about trigraphs. I bet they go way back to the time when all computers used 7 bit ascii for everything. I have my Bondwell 16 in my attic (next to my Sinclair ZX81). It used 7 bit ascii and to be able to show scandinavian letters Å, Ä and Ö, it replaced [ \ ] with Ä Ö Å. On a very low system level. It was burnt into the 128k bios ROM.

I didn't code in C at that time, but I coded in Pascal. And instead of writing FooÄ16Å, I wrote Foo(. 16 .)

On a standard English/US machine that would be Foo[16]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This thread is obviously about nothing so heres a bunny with a pancake on its head. -kazzmir

Neil Black
Member #7,867
October 2006
avatar

if(Bad == true)
         goto Hell;

Hell:
   Death = true;
   Burn = true;
   Pain = true;

I am Mildly Annoying Man!
I barely witnessed it. I was half asleep at the time. At first I thought he had fallen out of bed, but then I slowly realized that he had, in fact, just jump-kicked his way into consciousness.
My blog!

Dustin Dettmer
Member #3,935
October 2003
avatar

Possumdude0 said:

if(Bad == true)
         goto Hell;

Hell:
   Death = true;
   Burn = true;
   Pain = true;

So we're all going to hell no matter what? :'(

ReyBrujo
Moderator
January 2001
avatar

Of course!

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Rampage
Member #3,035
December 2002
avatar

No, only if Bad == true

-R

ReyBrujo
Moderator
January 2001
avatar

But if Bad is not true, you fall back to the same place!

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Rampage
Member #3,035
December 2002
avatar

Oops, how could I miss that? I'll blame it on almost never using goto. Yes, that was it...

-R

Bruce Perry
Member #270
April 2000

Quote:

And the way ">>" is treated when terminating nested template parameters.

Accepted in Java but not accepted in C++, I believe?

(Java also has to worry about ">>>" ;D)

Quote:

And the way variable initializations are sometimes treated as function declarations.

Eh? Got an example?

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
The brxybrytl has you.

Carrus85
Member #2,633
August 2002
avatar

Quote:

And the way ">>" is treated when terminating nested template parameters.

This is fixed in C++0x, by the way.

Krzysztof Kluczek
Member #4,191
January 2004
avatar

Quote:

This is fixed in C++0x, by the way.

What's here to fix? C/C++ parser builds longest symbols it can build, which I find to be very consistent behavior, so ">>" will always be interpreted as bit shifting operator and not as two separate greater-than's. :)

 1   2 


Go to: