-
goto
How do you use goto statements? The book I'm using doesn't cover it at all.
Please don't reply to this thread if you're going to say: "You should never use goto statements in your code. There are always better solutions using loops than to use gotos."
I remember a while back hearing something about a label...:confused:
-
A Little example will probably help you best:
Code:
// This Code Comes Straight From:
// Sams Teach Yourself C++ in 21 Days 3rd Edition
// Jesse Liberty
#include <iostream.h>
int main()
{
int counter = 0;
loop: counter++;
if(counter < 5)
goto loop;
return 0;
}
I have to say it though. Don;t use em. They are junk... Nothing but junk... Like little pieces of chewing gum under a chair it is just something that is C++. Nobody wants it there, nobody likes it there and nobody understands why they put it there.....
-
Isn't there an ingenious way to use goto statements? Wasn't an operating system programmed with a couple of goto statements in the code? Linux I believe...
-
I know you don't want this:
but since DirX already posted an example.
goto is undesired. it leaves to often confusing and sloppy code.
-
You have a base of truth there. Sometimes goto statements are the only way to go (like there are those people who like to take the chewing gum from under chairs and stick it in ther mouths again :rolleyes:). But generally C++ leaves you with other options. And on that linux comment, Big projects will always get messy and people will always make stupid mistakes... Goto statements are just messy as hell and make the code terrible to read...
-
Usually goto statements can be replaced by some type of loop (for, while, do..while).
-
I'd personally rather find a way to do it without 'goto' just for the simplicity of reading the code. However, it wont make your code unstable, and it isn't unprofessional either. You can use whatever method that you want.