Why Ignore GOTO
Was wondering, what's with everyone saying NEVER!!! use goto.
I mean , i always use it, it is very good, for speeding up your code, so why ignore it??
Because it makes your code like spaghetti. Here's an example:
Difficult to follow, yes? A bare 18 lines, and difficult to read. Now, imagine 200,000 lines of that!
While goto can be a powerful tool when used correctly (mostly for optimization), but it should not be used until need be, and only then for speed reasons. It has been proven that any algorithm can be implemented without goto (at least, that's what people say).
#include <iostream>using namespace std;int main (){ int x = 5; int y = 0; cout << "Input a number: "; cin << y; if (y == x) goto yes; else goto no; yes: cout << "You inputed 5!" << endl; goto end; no: cout << "You inputed " << y << "!" << endl; end: return 0;}
Difficult to follow, yes? A bare 18 lines, and difficult to read. Now, imagine 200,000 lines of that!
While goto can be a powerful tool when used correctly (mostly for optimization), but it should not be used until need be, and only then for speed reasons. It has been proven that any algorithm can be implemented without goto (at least, that's what people say).
Quote:Original post by bikola_p
I mean , i always use it, it is very good, for speeding up your code, so why ignore it??
A bit dated but: Go To Statement Considered Harmful.
I wouldn't say never use it. But don't use it unless you are in a situation where you need to (this very rarely happens, and can often be avoided with better design).
Quote:Original post by bikola_p
it is very good, for speeding up your code
Hm. While goto has its uses, I can't think of many situations where it would actually improve performance. Can you give a common example of how you'd use it for this?
in a switch statement eg
Ok in that example you are getting info such as wages etc from some file, and you are working with the data through the whole program, and you are given a step a time option rather then all being done in one thing.
Anyway, you can also do a loop on the outside of the main and have the case inside, but isnt the goto more convinient?, yes.
main: system("cls"); cout<<"1.Get Hr/pay from file \n 2.Calculate something read from file\n"; cout<<"3.print results \n 4.Exit"; cin<<choice; switch(choice) { case(1): { run() goto main; } case(2): { calculate(); goto main; } case(3): { print(); goto main; } case(4): { return 0; } defualt: { cout<<Please re-enter choice correctly" goto main; }
Ok in that example you are getting info such as wages etc from some file, and you are working with the data through the whole program, and you are given a step a time option rather then all being done in one thing.
Anyway, you can also do a loop on the outside of the main and have the case inside, but isnt the goto more convinient?, yes.
Quote:
Anyway, you can also do a loop on the outside of the main and have the case inside, but isnt the goto more convinient?
No?
Quote:
Anyway, you can also do a loop on the outside of the main and have the case inside, but isnt the goto more convinient?, yes.
How is it more convenient? Here's the code without a single goto.
do{ cout<<"1.Get Hr/pay from file \n 2.Calculate something read from file\n"; cout<<"3.print results \n 4.Exit"; cin<<choice; switch(choice) { case(1): run(); break; case(2): calculate(); break; case(3): print(); break; case(4): // do..while loop is exited break; default: cout<<Please re-enter choice correctly"; break; }} while(choice != 4);
You don't have to remember what line the label "main" points to, you don't have to worry about the compiler refusing to compile your code because of the way goto is messing with object initializations, and you're using constructs that anyone else who sees your code will immediately recognize. And the code is every bit as efficient as the version with gotos.
Quote:Original post by kayX
But using goto statement i can reduce a conditional checking isnt it?
What do you mean? Give an example.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement