The goto Statement

I l @ ve RuBoard

The goto Statement

The goto statement, bulwark of the older versions of BASIC and FORTRAN, is available in C. However, C, unlike those two languages, can get along quite well without it. Kernighan and Ritchie refer to the goto statement as "infinitely abusable" and suggest that it "be used sparingly, if at all." First, we will show you how to use goto . Then, we will show why you usually don't need to.

The goto statement has two parts : the goto and a label name . The label is named following the same convention used in naming a variable, as in this example:

 goto part2; 

For the preceding statement to work, the function must contain another statement bearing the part2 label. This is done by beginning a statement with the label name followed by a colon :

 part2: printf("Refined analysis:\n"); 

Avoiding goto

In principle, you never need to use the goto statement in a C program, but if you have a background in older versions of FORTRAN or BASIC, both of which require its use, you might have developed programming habits that depend on using the goto . To help you get over that dependence, we will outline some familiar goto situations and then show you a more C-like approach.

  1. Handling an if situation that requires more than one statement:

     if (size > 12)     goto a; goto b; a: cost = cost * 1.05;    flag = 2; b: bill = cost * flag; 

    In old-style BASIC and FORTRAN, only the single statement immediately following the if condition is attached to the if . No provision is made for blocks or compound statements. We have translated that pattern into the equivalent C. The standard C approach of using a compound statement or block is much easier to follow:

     if (size > 12) {      cost = cost * 1.05;      flag = 2; } bill = cost * flag; 
  2. Choosing from two alternatives:

     if (ibex > 14)    goto a; sheds = 2; goto b; a: sheds= 3; b: help = 2 * sheds; 

    Having the if else structure available allows C to express this choice more cleanly:

     if (ibex > 14)      sheds = 3; else      sheds = 2; help = 2 * sheds; 

    Indeed, newer versions of BASIC and FORTRAN have incorporated the else into their syntax.

  3. Setting up an indefinite loop:

     readin: scanf("%d", &score); if (score < O)    goto stage2; lots of statements; goto readin; stage2: more stuff; 

    Use a while loop instead:

     scanf("%d", &score); while (score <= 0) {      lots of statements;      scanf("%d", &score); } more stuff; 
  4. Skipping to the end of a loop: Use continue instead.

  5. Leaving a loop: Use break instead. Actually, break and continue are specialized forms of a goto . The advantages of using them are that their names tell you what they are supposed to do and that, because they don't use labels, there is no danger of putting a label in the wrong place.

  6. Leaping madly about to different parts of a program: DON'T!

There is one use of goto tolerated by many C practitioners : getting out of a nested set of loops if trouble shows up. (A single break gets you out of the innermost loop only.)

 while (funct > 0)     {     for (i = 1, i <= 100; i++)         {         for (j = 1; j <= 50; j++)             {             statements galore;             if (bit trouble)                 goto help;             statements;             }         more statements;         }     yet more statements;     } and more statements; help : bail out; 

As you can see from the other examples, the alternative forms are clearer than the goto forms. This difference grows even greater when you mix several of these situations. Which gotos are helping ifs , which are simulating if elses , which are controlling loops, which are just there because you have programmed yourself into a corner? By using gotos excessively, you create a labyrinth of program flow. If you aren't familiar with gotos , keep it that way. If you are used to using them, try to train yourself not to. Ironically, C, which doesn't need a goto , has a better goto than most languages because it enables you to use descriptive words for labels instead of numbers .

Summary: Program Jumps

Keywords:

 break, continue, goto 

General Comments:

These three instructions cause program flow to jump from one location of a program to another location.

The break Command:

The break command can be used with any of the three loop forms and with the switcyh statement. It causes program control to skip the rest of the loop or the switch containing it and to resume with the next command following the loop or switch .

Example:

 switch (number) {      case 4:  printf("That's a good choice.\n");               break;      case 5:  printf("That's a fair choice.\n");               break;      default: printf("That's a poor choice.\n"); } 

The continue Command:

The continue command can be used with any of the three loop forms but not with a switch . It causes program control to skip the remaining statements in a loop. For a while or for loop, the next loop cycle is started. For a do while loop, the exit condition is tested and then, if necessary, the next loop cycle is started.

Example:

 while ((ch = getchar())   != EOF) {      if (ch == ' ')           continue;      putchar(ch);      chcount++; } 

This fragment echoes and counts nonspace characters .

The goto Command:

A goto statement causes program control to jump to a statement bearing the indicated label. A colon is used to separate a labeled statement from its label. Label names follow the rules for variable names. The labeled statement can come either before or after the goto .

Form:

 goto label;     .     .     . label : statement 

Example:

 top : ch = getchar();       .       .       . if (ch != 'y')        goto top; 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net