The goto Statement

   


The goto Statement

The goto statement transfers program control to a position specified by the programmer. Consequently, it interferes with the usual sequential execution of a program.

The goto statement has had a long controversial history. One of the first languages to include this language construct was an early version of FORTRAN. However, in the late 1960s, most of the 1970s, and even presently at rare occasions, the pros and cons of this semi-structured language element are hotly debated among programmers and language designers.

Note

graphics/tnt.gif

Most programmers and computer language experts agree that indiscriminate use of the goto statement leads to ugly, unclear code that takes longer to write, contains more bugs, and is difficult to debug and maintain. As a general rule, goto is a superfluous language element in C#; however, it can be useful when constructing switch statements, as demonstrated in the next section. Because of this, the goto examples provided here are merely meant as illustrations of how goto works and are not examples of good programming practice. The goto presentation is included for completeness, to warn you against its use, to let you use it in the switch statement, and to let you know what's going on if you should stumble on the (hopefully) odd goto statement inserted by another programmer.


There are three varieties of goto statements. One variation (the goto label statement) is used in the general source code and is discussed in this section; the other two (the goto case and goto default statements) are specifically targeted at the switch statement and are discussed in the next section.

The goto label construct consists of a goto-label statement and an associated label statement. When the goto statement is executed, it transfers the flow of control to the associated label, as illustrated in Figure 8.20.

Figure 8.20. Transfer of control with the goto label statement.
graphics/08fig20.gif

Syntax Box 8.9 The goto-label-statement

 goto_label_statement::=          goto <Label>; Label_statement::=         <Label_identifier>:                <Statement>; | <Compound_statement> 

Note:

A <Statement> or <Compound statement> must follow the label statement. Consequently, it is an error to position a label statement at the end of a block of code as in the following:

 {   ...     {         ...         goto labelB;         ...     }         ...     labelB:   //Invalid. } 

To satisfy the compiler, you can simply insert an empty statement (a semicolon) after the label statement.

In an attempt to curb the amount of damage that a goto label statement can cause, the following restrictions apply.

The goto label statement and its associated label statement must be within the same method, and the goto label statement must be within the scope of the label-statement.

Practically speaking, these rules only permit the goto label statement to transfer control out of a nested scope, not inside a nested scope. Thus, the following is invalid:


graphics/08infig07.gif

whereas the following is accepted by the compiler:


graphics/08infig08.gif

The goto statement belongs to a group of statements know as jump statements. Jump statements cause the flow of control to jump to another part of the program. Other jump statements found in C# are break, continue, return, and throw. You have only met the return statement so far, which terminates a method. break and continue will be introduced later in this chapter, and throw will be introduced in Chapter 19, "Exception Handling."


   


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

Similar book on Amazon

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