goto?

This topic is 4936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Well should i use goto or what to go to another place in the program?

Share on other sites
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]

Share on other sites
What language? In general, I would advice against it, unless you have no choice (like in old fashioned Basic).

Share on other sites
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?

Share on other sites
Quote:
 Original post by aleksi1578Well 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.

Share on other sites
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 ;)

Share on other sites
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.

Share on other sites
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.

Share on other sites
Thanks guys.. I think I got it.. I reply if I have problems with functions.. Thanks alot!

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633722
• Total Posts
3013547
×