Code About Us Tutorial


  Statements & Expression
    The while Statement
    The do ... while Statement
    The for Statement
    The break Statement
    The continue Statement
    The goto Statement
    The switch Statement
    Keywords
    Scope Resolution (::)
    Arithmatic Operators


   
 

Home > Statements & Expression > The goto Statement

November 27, 2009 6:13 pm

   

The goto Statement

This next statement is called the goto statement; it has some bad and some good characteristics. I'll tell you about both. First, we have all learned that the break statement, the continue statement, and the switch statement cause the control of the program to branch to a location other than where it would normally go. The destination of the branch is determined by the context:
  • break goes to the next statement outside the loop,
  • continue goes to the loop's continue condition, and
  • switch goes to the correct case constant.

Note: The use of goto statements is obscured therefor it should not be used at all. Although, it is part of the language construct, it is unsafe to use goto statements.
width=1
width=1

All three of these statements are called jump statements because they cause the control of the program to "jump over" other statements. The goto statement is another kind of jump statement. A label within the statement specifies its destination. You're probably wondering what a label is, well a label is simply an identifier followed by a colon. Labels work like the case statements inside a switch statement: they specify the destination of the jump. The following application will show you how to exit out of a loop with the goto statement.

 
 
#include <iostream.h>

void main() { int n; for(;;) { cout <<"Enter a number to be cubed, terminate with 0: "; cin >> n; cout << n <<" cubed is: " << n*n*n << "n"; if(n == 0) goto esc;

} esc: cout << endl; }

In this code, it makes an infinite loop but uses the goto statement to break out of the loop. As you can see, if the variable n is equal to 0, it calls the goto statement and it jumps to the esc: cout << endl;. It's a very simple concept. The bad part of the goto statement is that if it is abused, it leads to unstructured spaghetti code which is difficult to debug, so be careful when using the goto statement.Please Rate this Code:


    Comments for: The goto Statement
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
As stated in this page: I would like to say that the use of goto statement is not such a good idea. Infact you should NEVER use it unless you have no other choice.

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
As stated in this page: I would like to say that the use of goto statement is not such a good idea. Infact you should NEVER use it unless you have no other choice. says:
I disagree with the article about goto usage. Yes, there are some inherent danger to using gotos, and improper usage can cause problems. However, the same can be said for pointers and no one in there right mind would tell a C++ coder to never use pointers. With good understanding of your code, limited usage of the goto statement is A.O.K.

Adam [gthreeboy@yahoo.com] Posted: 2 times. says:
I disagree with the article about goto usage. Yes, there are some inherent danger to using gotos, and improper usage can cause problems. However, the same can be said for pointers and no one in there right mind would tell a C++ coder to never use pointers. With good understanding of your code, limited usage of the goto statement is A.O.K.

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Yes, you are right. But there is nothing special that a goto can do that a well executed for loop cant do. And that my friend, is much easier to debug!!

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Yes, you are right. But there is nothing special that a goto can do that a well executed for loop cant do. And that my friend, is much easier to debug!! says:
when I view this page, the page is so wide that I have to scroll so much and it is IMPOSSIBLE to read this whole page!!

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Yes, you are right. But there is nothing special that a goto can do that a well executed for loop cant do. And that my friend, is much easier to debug!! says:
when I view this page, the page is so wide that I have to scroll so much and it is IMPOSSIBLE to read this whole page!! says:
goto\'s are occasionally helpful in making your code clearer and more concise.

Say you have an operation which is composed of a series of operations, each of which might potentially fail. If one does fail, then you\'ve got to clean up after it and all the operations which preceded it in reverse order. The easiest way to do this is to write a complete sequence of cleanups which would be used if the final operation failed, and then use gotos from the failure tests in each operation to jump into the appropriate point in this sequence so that only the necessary cleanup operations are performed. This removes the need for repetition of the code/calls for the cleanup steps.

On the whole, though, I agree with Dijkstra.





Add Comment:
Name:
Email:


 «8» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.