An example of why goto is horrible

Here is a piece of code which does a horrible thing.

It runs the loop 10 times, then the goto statement is activated and jumps into the loop, and activates the printf statement once again (with the value of the loop index i being 11). Now i is checked against the loop conditional, and because it is greater than 10, the loop is exited again, and the goto statement is again activated. This continues infinitum.

int i;
for (i=1; i<=10; i=i+1)
    OMG: printf("%d where are you when you go to?\n", i);
goto OMG;

It is not he fact that the goto has been used, but rather how it has been used. C by no means should allow a goto to jump into a loop. But C is in reality a high level abstraction of assembler, in which goto’s are fairly standard.

However it is not the goto that is the problem, it is the programmer’s who use goto’s in such a manner that they produce unstructured code, making it extremely difficult to follow.

Advertisements

One thought on “An example of why goto is horrible

  1. codeinfig says:

    “However it is not the goto that is the problem, it is the programmer’s who use goto’s in such a manner that they produce unstructured code”

    nope! but you did name the problem: unstructured code. in a day where python does so much of the work that basic used to, i dont know why people still need to malign goto several times a year– the feature constantly blamed for the people that misuse it.

    naturally i realize its a feature that lends itself so readily to misuse– but by that logic (and to much greater harm otherwise) you shouldnt be able to buy the glue that sticks models together. that stuff isnt just harmful, it can kill you! but only if you badly misuse it.

    remember kids: goto doesnt harm programs– people do. ok, thats actually what you said, isnt it? but if *people* dont write *unstructured code*, then goto does no harm at all. still we talk about it, as if walking around making sure that every coffee cup is inscribed with the warning: “careful! hot coffee is hot!”

    what we should be doing (to really show the damage don)) is interviewing the poor souls whose careers were ruined, who had a chance to learn to code properly but alas! goto got them too early. think “reefer madness,” but instead of showing the dangers of recreational drugs, it shows the once-promising lives devestated and destroyed by goto… then maybe people will understand how serious it really is. personally im happy with a good while statement, but i did experiment with goto in college.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s