The goto


The goto is C#’s unconditional jump statement. When encountered, program flow jumps to the location specified by the goto. The statement fell out of favor with programmers many years ago because it encouraged the creation of “spaghetti code.” However, the goto is still occasionally—and sometimes effectively—used. This book will not make a judgment regarding its validity as a form of program control. It should be stated, however, that there are no programming situations that require the use of the goto statement—it is not an item necessary for making the language complete. Rather, it is a convenience that, if used wisely, can be of benefit in certain programming situations. As such, the goto is not used in this book outside of this section. The chief concern most programmers have about the goto is its tendency to clutter a program and render it nearly unreadable. However, there are times when the use of the goto can clarify program flow rather than confuse it.

The goto requires a label for operation. A label is a valid C# identifier followed by a colon. Furthermore, the label must be in the same method as the goto that uses it. For example, a loop from 1 to 100 could be written using a goto and a label, as shown here:

 x = 1; loop1:   x++;   if(x < 100) goto loop1;

The goto can also be used to jump to a case or default statement within a switch. Technically, the case and default statements of a switch are labels. Thus, they can be targets of a goto. However, the goto statement must be executed from within the switch. That is, you cannot use the goto to jump into a switch statement. Here is an example that illustrates goto with a switch:

 // Use goto with a switch. using System; class SwitchGoto {   public static void Main() {     for(int i=1; i < 5; i++) {       switch(i) {         case 1:           Console.WriteLine("In case 1");           goto case 3;         case 2:           Console.WriteLine("In case 2");           goto case 1;         case 3:           Console.WriteLine("In case 3");           goto default;         default:           Console.WriteLine("In default");           break;       }       Console.WriteLine();     } //    goto case 1; // Error! Can't jump into a switch.   } }

The output from the program is shown here:

 In case 1 In case 3 In default In case 2 In case 1 In case 3 In default In case 3 In default In default

Inside the switch, notice how the goto is used to jump to other case statements or to the default statement. Furthermore, notice that the case statements do not end with a break. Since the goto prevents one case from falling through to the next, the “no fall-through” is not violated, and there is no need for a break statement. As explained, it is not possible to use the goto to jump into a switch. If you remove the comment symbols from the start of this line:

 //    goto case 1; // Error! Can't jump into a switch.

the program will not compile. Frankly, using a goto with a switch can be useful in some special-case situations, but it is not recommended style in general.

One good use for the goto is to exit from a deeply nested routine. Here is a simple example:

 // Demonstrate the goto. using System; class Use_goto {   public static void Main() {     int i=0, j=0, k=0;     for(i=0; i < 10; i++) {       for(j=0; j < 10; j++ ) {         for(k=0; k < 10; k++) {           Console.WriteLine("i, j, k: " + i + " " + j + " " + k);           if(k == 3) goto stop;         }       }     } stop:     Console.WriteLine("Stopped! i, j, k: " + i + " " + j + " " + k);   } }

The output from the program is shown here:

 i, j, k: 0 0 0 i, j, k: 0 0 1 i, j, k: 0 0 2 i, j, k: 0 0 3 Stopped! i, j, k: 0 0 3

Eliminating the goto would force the use of three if and break statements. In this case, the goto simplifies the code. While this is a contrived example used for illustration, you can probably imagine situations in which a goto might be beneficial.

One last point: although you can jump out of a block (as the preceding example shows), you can’t use the goto to jump into a block.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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