Why is using goto so bad?

Started by
151 comments, last by MrPoopypants 21 years, 4 months ago
Hi guys I was working on one of my C++ assignments, desperately trying to find a quick way to get out of a nested loop when i recalled my old Ti-83 programming days when i would use GOTO a lot. I used this in my C++ project, and the teacher''s responce was: "we DO NOT use goto''s in this class." I thought it was a bit of an overreaction but he said it promoted bad programming practice and screws up debugging and went on to say it was from the original FORTRAN compiler and he doesnt know why the kept it. I think goto''s are wonderful, but what do you guys think/know about them? Thanks
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
Advertisement
theres nothing wrong with it... my teacher won''t even let me use break... i''m like wtf? anyways goto is fine as long as its clearly readable... obviously if you''re in 10 nested loops and need to go out 5, goto is the best option. Besides would anything work without the JMP command in asm?
quote:Original post by MrPoopypants
Hi guys
I was working on one of my C++ assignments, desperately trying to find a quick way to get out of a nested loop when i recalled my old Ti-83 programming days when i would use GOTO a lot. I used this in my C++ project, and the teacher''s responce was: "we DO NOT use goto''s in this class." I thought it was a bit of an overreaction but he said it promoted bad programming practice and screws up debugging and went on to say it was from the original FORTRAN compiler and he doesnt know why the kept it.
I think goto''s are wonderful, but what do you guys think/know about them?
Thanks




Your instructor is right, the Goto statement promotes bad programming habits. Heaven forbid you even have to debug reasonably large program that contains Goto''s, you have track down all the targets. If you get hired as a programmer chances are good other people will have to read your code and Goto statement turn you code into a rats nest.
Patrick
I used goto a LOT when programming in ASM (since you can''t do anything usefull in ASM without ''goto'' (jmp, jz, jnz, etc.)). When i switched to PHP, for a few weeks, I thought I am goign crazy, when I found out that there is no goto in PHP. Then, I got used to it. When i switched to C, I had no use for goto, and I do find it a thing to be avoided (but nto a FORBIDDEN thing). Basically, you can do anythign without a goto, so...

Height Map EditorEternal lands
quote:Original post by Anonymous Poster
if you''re in 10 nested loops and need to go out 5, goto is the best option.

If you''re in 10 nested loops, then you''re beyond help.
I personally code in a way that I don''t need goto. But as long as it''s easy to understand what you''re doing, and you only use it when there''s no better way, then yeah, ignore your instructor and use goto anyways.

- Andy Oxfeld
I think this is another pointless religious debate. It''s just that 98% of programmers are on the same side. I think goto is useful in certain places. The problem is over use. I wouldn''t use more than one goto in a single function, and it would be clearly marked with comments. I would only use it to make the code cleaner and more readable.
__________________________________________________________America seems to like crap because its what we make popular. - Goober King
quote:Original post by SabreMan
If you''re in 10 nested loops, then you''re beyond help.

Unless you''re using a powerloop.


char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
The way languages are designed today, the GOTO becomes obsolete. Any loop that is made with a GOTO can be made using IF,WHILE,FOR and DO statements. That is why these statements exist, they are substitutes for GOTO's. The only time GOTOs are useful is at the assembly level (Branches and jumps). Dijkstra wrote a good paper on it.

[edited by - MaximuS_X on December 17, 2002 5:40:24 PM]
---
I only use goto when adding conditions to each of a set of nested loops makes it too confusing to be reasnoble.

- Your Local Drunk
- Your Local Drunk

This topic is closed to new replies.

Advertisement