Jump to content

Goto

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
Void

Void

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 411 posts
Can GOTO still be used in C or C++? Ive always read that this shouldn't be used but why? Why is it there if it shouldn't be used? Is it still there?


if (statement) {

  Goto 102;

}


I know it was used in the old dos basic programming language because there was no functions or classes.
Void

#2
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
Yes, you can use goto in C or C++. I'm not sure what the command is

If you use GOTO to much it becomes hard to read the code. It makes the code very unreliable as you don't know whats doing what.

#3
Guest_Axter_*

Guest_Axter_*
  • Guests
You should avoid goto, because it's easy to make spaguetti code, and easy to break OO design.

>>Why is it there if it shouldn't be used? Is it still there?
One of my favorite quotes from a C++ Author (Jack Reeves):
********************************************
I am not particularly interested in programming languages where the designer has deliberately decided I don't need something. I prefer programming languages that give me features that might be useful and let me decide what I need in a given situatuion.
********************************************

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests

Axter said:

You should avoid goto, because it's easy to make spaguetti code, and easy to break OO design.

>>Why is it there if it shouldn't be used? Is it still there?
One of my favorite quotes from a C++ Author (Jack Reeves):
********************************************
I am not particularly interested in programming languages where the designer has deliberately decided I don't need something. I prefer programming languages that give me features that might be useful and let me decide what I need in a given situatuion.
********************************************

Agreed! Nice post. You may not even be able to read your own code if you use goto to often (or at all). Use functions instead


if (statement) {

  Do102();

}


Same concept but much easier to read and follow.

#5
Void

Void

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 411 posts
I know about functions I was more interested in why it was still there. I think Axter answered it fairly well.
Void

#6
DevilsCharm

DevilsCharm

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
I think it's just there because it has to be there. I mean, if GOTO wasn't there, and someone needed to use it (I don't know why... but maybe), and it wasn't available, that would be bad. I've used the GOTO command in another language I was working with, but I only did it for the purpose of doing it (you know, just to try it out).

#7
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
I actually use goto in C#/VB.NET/Java occassionally to implement retries:


TryLabel:

tries = 0;

try {

   tries++;

   this.ThrowsAnException();

} catch ex as TheException {

   if (tries < 3) {

      goto TryLabel;

   } else {

      throw;

   }

}


I'd prefer a ReTry construct, but until Anders decides to give it to me, I haven't found anything cleaner.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

brackett said:

I actually use goto in C#/VB.NET/Java occassionally to implement retries:


TryLabel:

tries = 0;

try {

   tries++;

   this.ThrowsAnException();

} catch ex as TheException {

   if (tries < 3) {

      goto TryLabel;

   } else {

      throw;

   }

}


I'd prefer a ReTry construct, but until Anders decides to give it to me, I haven't found anything cleaner.

This is an example of where you don't need a goto statement.




int tries = 0;

bool tryAgain = false;

while ((tries < 3) && !tryAgain)

{

  tryAgain=false

  try {

     tries++;

     this.ThrowsAnException();

  } catch ex as TheException {

     tryAgain=true 

  }

}

if (tryAgain) throw;



#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

Void said:

Can GOTO still be used in C or C++? Ive always read that this shouldn't be used but why? Why is it there if it shouldn't be used? Is it still there?

I know it was used in the old dos basic programming language because there was no functions or classes.

It's included, I think, for backwards compatibility. The reason it's "bad" is because it tends to violate all the principles of structured programming that C and C++ try to exemplify.

The biggest problem is that you can jump between scopes and miss things like variable declarations, etc.

int main()
{
  goto skip
  int i = 5;
  skip:
  i++;  //error! i not declared!
  return i; //error!  i not declared!
}


#10
yhu221300

yhu221300

    Newbie

  • Members
  • Pip
  • 1 posts
You can use goto occasionally, but avoid using it if possible since it breaks the normal procedure.

#11
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts

WingedPanther said:

This is an example of where you don't need a goto statement.




int tries = 0;

bool tryAgain = false;

while ((tries < 3) && !tryAgain)

{

  tryAgain=false

  try {

     tries++;

     this.ThrowsAnException();

  } catch ex as TheException {

     tryAgain=true 

  }

}

if (tryAgain) throw;


I'm not sure that's any cleaner. And I don't think that throw will work, since you already swallowed the exception and are out of the catch block.

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
There are techniques for rethrowing an exception. I'd have to look up how to do that.

I will say that in Stroustrup's book The C++ Programming Language, he admits that there are times when a goto is appropriate, but mainly in low level code that is well hidden from the standard interaction by users of the code.