Template:Computer Programming/Control/5

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Isn't goto evil?[edit]

One often hears that goto is evil and one should avoid using goto. But it is often overlooked that any return which is not the last statement inside a procedure or function is also an unconditional statement — a goto in disguise. There is an important difference though: a return is a forward only use of goto. Exceptions are also a type of goto statement; worse, they need not specify where they are going to!

Therefore if you have functions and procedures with more than one return statement you can just as well use goto. In practice, nearly every programmer is familiar with a 'return' statement and its associated behavior; thus, when it comes down to readability the following two samples are almost the same: