goto?

Started by
139 comments, last by GameDev.net 18 years, 10 months ago
Well should i use goto or what to go to another place in the program?
Advertisement
Sometimes..
C has got about a dozen different control structures each useful in various situations, I suggest you try to get comfortable with all of them.

[Edited by - doynax on June 3, 2005 9:04:36 AM]
What language? In general, I would advice against it, unless you have no choice (like in old fashioned Basic).
Well I program in C++/C.. Please can you tell some other ways than goto? or any tutorials that has some of them/all of them?
Quote:Original post by aleksi1578
Well I program in C++/C.. Please can you tell some other ways than goto? or any tutorials that has some of them/all of them?
The function call is the basic method in C.
Try to find some introductory C book or equivalent online tutorials. A trip to your local library might not be a bad idea.
The obligatory link. :)

http://www.acm.org/classics/oct95/
Orin Tresnjak | Graphics ProgrammerBethesda Game StudiosStandard Disclaimer: My posts represent my opinions and not those of Bethesda/Zenimax, etc.
You should not use goto instruction, as it is deprecated and shows bad coding habbits. Instead, you need to learn how to use loops [do, do-while, while, for], function calls and exception handling [the latter is C++ only, and rather an advanced topic].

As Doynax said, try to find a good C / C++ book or online tutorial and code as much as you can ;)
Tempus FugitI'm an Object Oriented Programmer ;).My site [placeholder; rest is under development]Currently working on: Holodeck 2D Engine
That I can think of, gotos are never necessary in C++. And if I remember correctly, they don't obey scoping rules either. For example:
void SomeFunc(){  int i = 0;  while (true)  {    std::string s('!', 1024);    ++i;    if (i == 1)    {      goto end_of_func;    }  }  end_of_func:}

I'm pretty sure that the std::string object won't get properly destructed, since it never officially goes out of scope. This is bad.

What specifically are you trying to accomplish, because I'm confident that it can be rewritten or redesigned slightly to not use gotos.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
It's a bit better [luckily] :). As far as I know, with goto you cannot leave or enter a block, so:
void SomeFunc(){  int i = 0;  goto inside_loop;   //error, cannot enter block  while (true)  {    inside_loop:    std::string s('!', 1024);    ++i;    if (i == 1)    {      goto end_of_func; //error, cannot leave block    }  }  end_of_func:  goto end_of_func;    //ok, now it is valid -> infinite loop ^^}

As I said, I'm not 100% sure about it since I never use goto ;), but as far as I recall the label must be in the same block that the goto statement, not inside a subblock or superior block of code.
Tempus FugitI'm an Object Oriented Programmer ;).My site [placeholder; rest is under development]Currently working on: Holodeck 2D Engine
Thanks guys.. I think I got it.. I reply if I have problems with functions.. Thanks alot!

This topic is closed to new replies.

Advertisement