Thread: How do i use Goto?

  1. #1
    Unregistered
    Guest

    Unhappy How do i use Goto?

    I just learned switch/case in C++ and now i need to know how
    goto.I just need an example also.

    Thanx ;d

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    int main(void){
    
    int x;
    cout << "Choose; enter 1 or 2" << endl;
    cin >> x;
    if(x == 1) goto ONE;
    else if(x == 2) goto TWO;
    else goto OTHER;
    
    ONE:
    cout << "You picked 1" << endl;
    goto END;
    TWO:
    cout << "You picked 2" << endl;
    goto END;
    OTHER:
    cout << "You picked something other that 1 or 2" << endl;
    goto END;
    
    END:
    
    return 0;
    
    }
    That's a quick example.

    Do a search for more info if you need it.

    There was a thread on General Discussions that discussed goto recently.
    Last edited by Fordy; 01-05-2002 at 07:07 AM.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    isn't it bad style to use goto in c++? my old comp sci teacher told us if we use goto we get an automatic F.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    isn't it bad style to use goto in c++? my old comp sci teacher told us if we use goto we get an automatic F.


    As I said....go to the thread that took place on General Discussions a few weeks ago.....that went into the virtues of goto quite well

  5. #5
    Unregistered
    Guest
    >my old comp sci teacher told us if we use goto we get an automatic F.
    Your old comp sci teacher was a dumbass. Anyone who says to 'never ever' use goto obviously doesn't know how it works. The fact that your teacher was trying to push his/her opinions on you to effect your programming style proves this.

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Actually, mr. unregistered, that's incorrect.

    Goto is bad programming practice- VERY bad programming practice. It creates spaghetti programs. In C/C++, there is not a single thing that cannot be done another way. The _only_ time goto would be even considerable would be to get out of a heavy loop, but even then it is unwise to use it. Goto is a relic of the BASIC days.

  7. #7
    Unregistered
    Guest
    Yes, there are always a number of ways to do something but you are making anwarranted assumptions.

    >It creates spaghetti programs.
    No, goto doesn't do anything but what it's supposed to do. The programmer creates the spaghetti programs. If you used goto right then there wouldn't be any problem. Everyone says it's hard to follow a program that uses goto, but that's because the only programs most people have seen are examples that totally abuse it to scare you off. In a real life situation the function would be small enough to follow easily and only one or two gotos at the most would be used. Very easy to follow.

    goto is NOT a jump anywhere you want command, it's confined to the function in which it is called. I see no danger when it's used properly and I highly recommend you try thinking for yourself before trying to defend an opinion that was pressed onto you before researching and deciding for yourself if that opinion is correct. Find out what goto is all about and then come back and agrue with me.

    >Goto is bad programming practice- VERY bad programming practice.
    Why? Give me proof that by simply using a single goto in a tiny function you instantly turn the whole program into a nightmare. If you can't, then go and look up goto as well as setjmp and longjmp, try them out and stop wasting my time.

    >The _only_ time goto would be even considerable would be to get out of a heavy loop
    Really, I had no idea that was the only time. What if I need some high performace code that needs to be short as well as efficient. I could use functions but function calls take time and power, hey here's an idea. Use goto. Once again, research your topic before trying to defend your case.

    >Goto is a relic of the BASIC days.
    Oh man, I'm sorry, I had no idea that goto wasn't used anymore. I was under the impression that when your code is optimized and changed into machine instructions goto was used quite regularly. Oh wait, that's the way it does work. Do you know assembler? Because if all you know how to program is BASIC and C++ then obviously you might see goto as something that is just a relic. However, in the real world goto is everywhere, it's just covered up by higher level language routines such as break and continue.

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Well then...

    Please tell DSS and the ANSI/ISO committee that the following statements are all very incorrect.

    "Has the use of structure-breaking directives such as goto, continue and break been avoided? ( if so, get rid of them )"

    "Consider instead adding or changing the exit conditions of the
    the control statement. Do not use goto."

  9. #9
    Unregistered
    Guest
    >"Has the use of structure-breaking directives such as goto, continue and break been avoided? ( if so, get rid of them )"
    Aw damn, so I guess we have to stop using switch now too, huh? Since the implementation of a switch requires break for it to work then the ISO committee is far from being correct in this quote. I think you have taken it out of context just to prove a point that doesn't exist. Of course, this is coming from a standard that often contradicts itself so who knows.

    >"Consider instead adding or changing the exit conditions of the
    the control statement. Do not use goto."
    Hmm, since your last quote told me not to use continue or break then I find this one to be lacking in credibility. Did you take this out of context too? Because unless I'm mistaken I did say earlier that there are times goto should be avoided in light of better solutions. If I didn't then I'm doing so now.

    I'm not going to turn this into a quoting war because the standard is difficult to follow even when everything is in the proper context, if you think it's wrong then don't use it. But do NOT make others avoid it because of your opinions. If you must say something, suggest that they research and then do their own tests to see what they think.

  10. #10
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Actually... in many cases it's better to use if/if else than switch... But it's not that any of this is required, it's just that it's better programming practice

  11. #11
    Unregistered
    Guest
    You still haven't told me why it's bad programming practice. Read my previous posts just in case you decide to reply with an argument that I've already covered btw, such as spaghetti code.

  12. #12
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    It's pretty much an absolute jump... Either way, you haven't told me why using goto is better, or even at the same level of effieciency, of using the rest of C++ conditional and structural abilities such as for next, if/if else, do, while, etc..?

  13. #13
    Unregistered
    Guest
    If you mean better by goto being the best choice in place of if/else and loops then you're not listening to my argument. In my experience goto has never been the best choice, I say never because I'm sure that I haven't found it to be the best but you say never in dictating how others should program, note the difference. This doesn't mean I'm not willing to use goto, it just means that I've never had an occasion to.

    I don't recommend using goto in place of all loops and conditional tests, that's just stupid because everyone knows how to read those with ease. You can use goto to replace function calls and gain a similar break up of the code so that it's easy to read and gain a huge increase in efficiency. A function call takes up resources but a jump with goto is unconditional, it does it right away and takes up little resources.

    You seem to think I like goto and use it all the time, this isn't the case. I feel that goto is a tool that can be used correctly or incorrectly and I despise how everyone tries to dictate to everyone else how to program. I believe that goto is a good tool because the standard tells us exactly how it works and it works that way. If you can prove to me that goto is bad programming practice in any way other than programmer opinion, I'll change my opinion. It's like void main(), I can prove that void main() is undefined, can you do this with goto?

    When I think of the goto controversy I believe I can sum up why everyone hates it in two simple words. User Error. If you don't use it correctly, it'll bite you in the ass, just like pointers. I don't care how you program, you shouldn't care how I program, but I don't force others to program like I do and you shouldn't either.

    That's my argument, what's yours?

  14. #14
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    My argument is simple and intelligent; If people don't listen to what I say, and ONLY to what I say, then they r sux0r n00bz.


  15. #15
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You guys need to relax and realize what u r arguing about, computer programming. Think about it!! If one does not want to use it, let them. But do not enforce ur views on other impressionable minds. I believe that using goto is similar to starting a sentence with because. It is taught early on not to do it because it could lead to incomplete sentences or bad code. However, when used correctly, it can be effective. The only time I would frown on using goto is in very large programs where it would be extremely unefficient or using it as the substitution of other preferred methods such as loops or function calls. Because this is only my opinion, many may disagree and that is ur right. But, do not criticize others for their views.

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