OK, i know it's concidered bad practace to use GOTO. but why?
OK, i know it's concidered bad practace to use GOTO. but why?
It allows you to jump anywhere in the code of the project back, forward, this way, thatway. Its not that it doent work, its just it makes the code extreamly difficult to read, even if you are the original coder.......
Its funny that whatever book you read on C/C++, they all dedicate a section as to why goto should not be used......I've seen the same in a Delphi book too...... I guess the reason why its still supported is that it works ok...i'ts just deemed bad practice....
GOTO leads to spaghetti code.
The only place I see goto anymore is action script, where your
code can't turn into pasta.
rick barclay
No. Wait. Don't hang up!
This is America calling!
Not true....It allows you to jump anywhere in the code of the project back, forward, this way, thatway.
Goto only allows you to go to a label that is in the same function as the goto statement.
It can be useful to escape deeply nested loops but has little or no other uses.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
I don't think there is anything wrong with goto. I use it just as I would with any other code. I find it quite convenient. It is an easy way to 'jump'.
--Garfield
1978 Silver Anniversary Corvette
goto is okay as long as you don't overuse it and complicate the code. Personally I don't use it because I prefer to come up with more creative solutions to my problems so that I'll know another way to do it later on.
C code. C code run. Run code, run...please!
>>Goto only allows you to go to a label that is in the same function as the goto statement.
Really...didnt know that it goes out of scope.....suppose thats cuz the books dont give it coverage...
I've heard that a goto statement is exactly the same jmp in assembly, so, by using goto, you're making your program even more low-level (which can be good or not), but I learnt programming without goto, it's more challenging.
Oskilian
Well you can still can program structured programs with
goto. You just have to be careful that you aren't jumping everywhere.
So here would be a while loop
This is only useful when you don't have a while loopCode:goto cond; loop: printf("Hello World!\n"); x++; cond: if (x < 5) goto loop;
in a language such as assembly.
Here are the reasons why I don't use gotos.
First of all, something is wrong with my compiler, and it won't work correctly, so I can't.
Second of all, if you wanted to call to a different location, just use a function call. Functions do just as much as gotos, and more.
Third of all, if I wanted to loop, I'll do it the old fashioned way with a do, while, or for loop.
What will people say if they hear that I'm a Jesus freak?
What will people do if they find that it's true?
I don't really care if they label me a Jesus freak, there is no disguising the truth!
Jesus Freak, D.C. Talk
-gnu-ehacks
Get a different compiler.First of all, something is wrong with my compiler, and it won't work correctly, so I can't
Functions when finished return control back to the caller. Goto is an unconditional jump.Totally different.Second of all, if you wanted to call to a different location, just use a function call. Functions do just as much as gotos, and more.
know a better way of doing this....
for(......)
for(......)
for(......)
for(......)
{
if(suchandsuch) goto OutOfLoop;
....
....
....
}
OutOfLoop : // do stuff
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
It leads to code that jumps around - yes they may be labelled but if you have 8000 lines of code it is difficult to trace.
It makes debugging very hard - when you use disassemblers and are faced with endless jmp commands
you are missing the point. with goto you can only jump to somewhere else that is within the same function as the goto. Used sparingly it is a powerful and helpful tool.It is not a keyword that i would like to see removed from the language. debugging is not made difficult because if you cant logically follow a function then chances are that its too large anyway and in need of further modularization.It leads to code that jumps around - yes they may be labelled but if you have 8000 lines of code it is difficult to trace.
So you wouldn't like to see while loops, or do while loops or for loops etc. Do you think these are implemented in assembly any other way than with jmp or related instructions?It makes debugging very hard - when you use disassemblers and are faced with endless jmp commands
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Goto function is not good, beacuse source code is not easily "scanned".
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Why not? If you have a line like this:
...code...
goto label;
...code...
And you absolutely can't find the "label", then there is a nice function to search for a word through source. Not too hard.
--Garfield
1978 Silver Anniversary Corvette