Branching Within Code Using goto

   

graphics/newterm.gif

Decision structures are used to selectively execute code. When a decision statement is encountered , C# evaluates an expression and diverts code according to the result. You don't have to use a decision structure to divert code, however, because C# includes a statement that can be used to jump code execution to a predetermined location within the current procedure: the goto statement. Before I talk about how to use goto, I want to say that under most circumstances, it's considered bad coding practice to use a goto. Code that's heavily laden with gotos is difficult to read and debug because the execution path is so convoluted. Such code is often called spaghetti code, and should be avoided at all costs. I'd say that in 90% of the situations in which goto is used, a better approach to the problem exists, and I'll show an example of just such a case shortly. Nevertheless, goto, like all other statements, is a tool. Although it's not needed as often as some of the other C# statements are, it's still a useful tool to have at your disposal ”when used judiciously.

graphics/newterm.gif

To jump to a specific location in code, you must first define the jump location using a code label. A code label is not the same as a Label control that you place on a form. You create a code label by positioning the cursor on a new line in a method, typing in a name for the label followed by a colon , and pressing Enter. Code labels can't contain spaces and they can't be a C# reserved word. For instance, you can't create a code label called try, because try is a reserved word in C#. However, you could create a label called TryThis, because TryThis isn't a reserved word. Code labels act as pointers that you can jump to using goto. The following shows an example using goto to jump code execution to a label.

 private void btnGoto_Click(object sender, System.EventArgs e) {    long lngCounter = 0; IncrementCounter:    lngCounter++;    if (lngCounter < 5000) goto IncrementCounter; } 

This code does the following:

  • Dimensions a long variable called lngCounter.

  • Sets the new variable to 0.

  • Defines a code label titled IncrementCounter. One or more goto statements can be used to jump code execution to this label at any time.

  • Increments lngCounter by 1.

  • Uses an if statement to determine if lngCounter has exceeded 5000. If it hasn't, a goto statement forces code execution back to the IncrementCounter label, where lngCounter is incremented and tested again, creating a loop.

This code works, and you're welcome to try it. However, this is terrible code. Remember how I said that the use of a goto can often be replaced by a better coding approach? In this case, C# has specific looping constructs that you'll learn about in the next hour . These looping constructs are far superior to building your own loop under most conditions, so you should avoid building a loop using a goto statement. In fact, one of the biggest misuses of goto is using it in place of one of C#'s internal looping constructs. In case you're interested, here's the loop that would replace the use of goto in this example:

 for(long lngCounter = 0; lngCounter<=5000; lngCounter++)    ... 

This discussion may leave you wondering why you would ever use goto. One situation in which I commonly use goto statements is to create single exit points. As you know, you can force execution to leave a method at any time using return. Often, clean-up code is required before a method exits. In a long method, you may have many return statements. However, such a method can be a problem to debug because clean-up code may not be run under all circumstances. Because all methods have a single entry point, it makes sense to give them a single exit point. With a single exit point, you use a goto statement to go to the exit point, rather than use a return statement. The following procedure illustrates using goto to create a single exit point:

 private void btnGoto_Click(object sender, System.EventArgs e) {    ...    ...    // If it is necessary to exit the code, perform a goto to    // the PROC_EXIT label, rather than using an Exit statement. PROC_EXIT:    ...    return; } 

   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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