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]
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 aleksi1578The function call is the basic method in C.
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?
Try to find some introductory C book or equivalent online tutorials. A trip to your local library might not be a bad idea.
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 ;)
As Doynax said, try to find a good C / C++ book or online tutorial and code as much as you can ;)
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:
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.
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.
It's a bit better [luckily] :). As far as I know, with goto you cannot leave or enter a block, so:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement