Thread: but why not GOTO?

  1. #1
    LonelyPlanetWanderer
    Guest

    but why not GOTO?

    there probably ain't no anology here but without the goto function i wouldn't 've got anywhere with my flash project...so it just seems kinda weird why we've been asked not to use the GOTO function in C...would appreciate if some1 could tell me why does structured programming forbid the usage of the goto statement?...does the last sentence hold true for ur side of the planet too?...any thoughts/suggestions would be greatly appreciated as always?

  2. #2
    Unregistered
    Guest
    well, the reason I've always heard is that use of GOTO makes code harder to follow when reading it and thus more difficult to maintain... I've also heard people refer to it as a crutch since you can virtually always get the same functionality without using GOTO.

  3. #3
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    The only people I've heard say never to use goto are the ones who don't understand it properly, they're just saying to others what was told to them. Yes, goto can cause difficult to follow code, but it's not something that magically makes your code terrible just because you use it once. K&R said it best when it was defined as "infinitely-abusable". Meaning that the danger in goto is solely caused by the programmer. If you know how to use it and do so in such a way as to make it easier to understand then goto is a valuable tool.

    The fact of the matter is that jumps are used whether you put that keyword in your code or not, and goto does have limits; it can only be used inside a single function, you can't use it to jump across functions. Everyone bashes goto, but setjmp and longjmp do the same thing but have a scope that spans the entire program. Is it just me or does that sound like the one to watch instead of goto?

    What's more, goto can be used to replace funtion calls to create high performance code when needed. Since calling a function has overhead and a local jump doesn't have nearly as much, you'll see goto used in code that requires very fast processing. As long as it's written in such a way as to not be confusing then I don't see a problem with it.

    In conclusion, ignore people who say to never use goto, instead use it sparingly and carefully.

    Which is easier to read? This?
    Code:
    int main( void ) {
        someFunction();
        someOtherFunction();
    
        return 0;
    }
    
    void someFunction( void ) {
        someOtherFunction();
    }
    
    void someOtherFunction( void ) {
    }
    Or this?
    Code:
    int main( void ){
        goto label1;  /*first function call from main*/
        goto label2;  /*second call from first function*/
        goto label2;  /*final call from main*/
    
        label 1:
        label 2:
        
        return 0;
    }
    Same effect and they're both easy to follow...so what's the problem?
    Last edited by *pointer; 12-21-2001 at 01:53 PM.
    pointer = NULL

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The real problem with goto is when you're debugging.

    Incidently, your example is lousy. Personally, it's much easier to read a function call than to find out where the label leads you. Additionally, your example doesn't work because your lables don't do anything. In your example, there is no reason for goto, so it's a poor example. No one would use goto in that situation.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Heh, no one would use goto in that situation at all. Notice that the functions do nothing as well, I was trying to save space by making it sort of like a fill in the blank.

    What kind of problems arise in debugging?
    pointer = NULL

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It mainly has to do with tracking the values of variables, to see why the program pukes.
    Code:
    int main ( void )
    {
        int x; goto lab1;
       lable ex:
       return 0;
       lable lab2: --x; goto lable lab5:
       lable lab1: x =1;
       lable lab4: ++x;
       switch( x ) {
           case 1: lable lab5: x += 3; goto lab3;
           case 2: lable lab6: x--; goto lab8;
           case 3: lable lab7: x+= x; goto lab4;
           case 4: lable lab3: x -= x; goto lab6;
           case 7: goto ex;
           default: lable 8: goto lab9;
       };
       lable lab9: x-=2;
       goto lab2;
    }
    What is the value of x when the program terminates?
    The same sort of thing happens when you're debugging. It's hard to find out what all the values of the variables are when you keep jumping scope etc.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    It is perfectly legal to use goto, and in some situations it may be desired. I don't use it for 2 reasons:

    1.) I like to think of better ways to do things than spaghetti code. It I wanted that I would use BASIC.

    2.) I feel bad for anyone who has to read my code and look at all the goto's... what a pain that would be. I would refuse to debug someone's code if they used a bunch of goto statements.

    That is my two cents
    one fish two fish
    red fish blue fish

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Personally, the 'problem' with gotos is just that it breaks the structure of code. Usually, if you can code the same thing using if or for, then it will look better, and it will not interrupt your indentation.
    While I don't think it should be illegal (feel free to use a goto if you really mean goto), be careful that you're not doing something that would look pretty much the same using if.

    In my experience, there have been two reasons to use goto...
    1)
    Code:
    for (;;)
    {
     scanf ("%d ", &x);
     switch (x)
     {
      case 1: // blarg
       break;
      case 2: // bloo
       break;
      case 3: // exit for loop
       break;  // obviously not neccisary, but makes a point...
      default: // boggiltyboo!
       break;
     }
    }
    exitfor: // BLEE
    Ah yes, the forever loop. Typically, this is broken out of using a break. However, within a switch, or some other constructs, like maybe another loop, you can't use a break. In this case, it might be helpful to just use a goto exitfor; It could be done also by changing the structure of this code, but the code is already pretty clean, and it isn't that hard to figure the meaning of the line goto exitfor;

    Also, sometimes I want to return from a function, but there are things that I absolutely must do every time I exit from the function. Consider the following code...
    Code:
    void doStuff (int i)
    {
     // Lots of code
     // End of function...
    
    ex: 
     popStuff();
     freeMemories();
     closeFiles();
     hidePasswords();
     return;
    }
    Now, without goto statements, there are two ways I can handle this. First off, I can just go ahead and perform these operations for every case where I want to escape the function:
    Code:
    // Code here.
    if (something)
    {
     popStuff();
     freeMemories();
     closeFiles();
     hidePasswords();
     return;
    }
    // Code here too.
    // End of function...
    And actually, if we encapsulate all this in a macro, then it wouldn't really be all that hard to write. However, this also leads to having a lot of redundant code.

    The other way would be just using if not statements...
    Code:
    //Code here.
    if (!something)
    {
     //Code here too.
    }
    // End of function...
    This is exactly the same as using the goto statement, except for needing a label. This method can result in problems however, since every catch is going to add one more indention to your code (this can clearly start a lot of problems if you have more than one of these), and it is in some situations just a strange construct, having a trivial test have a block over such a large piece of code. Also, this method just can't be used in all cases.

    Even in light of these reasons, the goto command shows up rarely (practically never) in my code. It just doesn't happen all that often, and if it does often in your code, then it is possible that you are just writing programs in a bad way. If you are using a goto statement, just make sure that it makes sense to use a goto there, that you don't already have very many gotos in the function (if you are running out of ideas for label names, then that's a clue), and that the label makes the goto meaningful...
    goto escapeFunction;
    makes sense
    goto label3;
    means nothing.

    quzah: I'm not sure if that's really a fair example... That code is obfuscated, uses incorrect syntax, and is an endless loop.
    Not to mention, I get the feeling that that code would be even harder to understand without using gotos, which would mean it is a good example of when to use gotos.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    quzah: I'm not sure if that's really a fair example... That code is obfuscated, uses incorrect syntax, and is an endless loop. 
    Not to mention, I get the feeling that that code would be even harder to understand without using gotos, which would mean it is a good example of when to use gotos.
    You're right on a few respects:
    1) the syntax was wrong. There is a reason for this: I have never EVER had a need to use 'goto'. As such, I never bothered paying attention to their syntax. The word 'label' is not needed.

    2) That code was meant to be obfuscated. The point of it however, was to show that using goto can make debugging a true pain in the ass. Since everything else in C is prodedural, jumping from here to there with a goto can definatly break the flow of the process.

    3) Yeah, it was an infinite loop. I just threw a bunch of crap together to show what a head ache it can be.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    LonelyPlanetWanderer
    Guest

    thx a ton but.....

    greatly appreciate all ur replies...helped a ton in getting things in perspective...but i reckon, and i might be wrong, that *pointer has a point there on the overhead of function calling as compared to local jumps...any ideas on that?...how true is that?

    *pointer: who's K&R?...please enlighten...

    cheers boyz!

  11. #11
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Function calls cause overhead, local jumps do not. However, function calls are used most often and people can read them easier; and as quzah said there can be problems with debuging. It's really all a matter of preference. Personally, I've never needed to use goto and I doubt that I will for quite some time.

    K&R is THE book on C, The C Programming Language by Brian Kernighan and Dennis Ritchie
    pointer = NULL

  12. #12
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    I've never had to use it either, but I do ASM (and just finished a forced class on VB) and there it's pretty much one of the most important things, only instead of goto there're jmp, jnz, jz...x 1 billion other forms each doing something different.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This has been debated time and again anywhere you wish to look. I believe that the correct answer to the goto problem is as follows:

    goto, just like any other keyword, is a tool. This tool can be abused or it can be used properly just like everything else. One can write fugly code with if/else just as easily as with goto. Similar to pointers, C gives us many weapons to blow off our feet if we don't use them with caution. I believe that goto is a crutch for bad designing and should be used sparingly, if at all. But there have been times when writing code that needs to run FAST that I've called upon goto to help.

    It's there when we need it, but try to find another solution first and everyone will be happy.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    Another way to jump would be to use longjmp(), at least then you have to include the jumping header file, thus warning anyone using your code that there are jumps. It is a little messier though and it really jumps back to that part of your program, like the rest never happened....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM