Thread: Goto statements, good or bad?

  1. #1
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100

    Goto statements, good or bad?

    Everyone I talk to says Goto statements are bad programming but I have yet to figure out a way to return to a line of code that I have passed in the program. The only way I know of to go back up the code is to do this:

    A:
    blah();
    blah();
    cout<<"Blah";
    goto A;

    thats the only way i can think of to go back up to the start, how else would you do it? I know that people use for while loops and such but again, how would you go back up the code? Please help me turn from the dark side.
    Signature is optional, I didnt opt for one.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    If it works, go for it.
    Its just reading the code becomes difficult when you use goto commands. You dont know where the goto is going and where its come from on big programs.

    But to answer your question on alternative solutions there really is a simple one. Loops.

    You could use a while loop for that example

    char user_selection;
    while (user_selection != 'Y')
    {
    blah();
    Blah();
    cout << "whatever";
    count << "Do you want to continue?"
    char = getch();
    }

    This exits when the user enters in a Y. It loops again otherwise.

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > If it works, go for it.

    No.

    The while loop idea's the right way.

  4. #4
    Unregistered
    Guest
    I am extremely thankful to my computer science teacher for not even bothering to grade work with goto statements. goto statements are bad form, and will lead to bad code, and worse, will preclude you from ever getting a serious computer programming job.

    Break the habit ASAP, because if you get used to using goto controls, it will just be harder to get into proper structured programming.

  5. #5
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    Umm, ok, that works for small loops but what if I need to do this:

    A:
    blah();
    blah();
    blah();
    if (something==something)
    goto B;
    else
    goto A;
    B:
    blah_blah();
    goto A;

    how would i make a loop to do that?
    Signature is optional, I didnt opt for one.

  6. #6
    Unregistered
    Guest
    Code:
    while (TRUE) {
       blah();
       blah();
       blah();
       if (something==something){
          blah_blah();
       }
    }

  7. #7
    Unregistered
    Guest
    If you are trying to go up, think of

    { as the label

    } as the goto

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    ok now what if you are trying to do a selection like:

    char sux[9];

    cout<<"life sux does it not?";
    cin>>sux[9]

    if (sux[9]==yes)
    {
    cout<<"you need some fun in your life go play solitare."
    goto solitare;
    }
    else if (sux[9]==no)
    {
    cout<<"you are too optimistic. Go play Death march."
    goto deathmarch;
    }
    else
    cout<<"your mother raised you well. Lets play Mommasboy."
    goto Mommasboy

    solitare:
    cout<<"blah";
    blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah;

    deathmarch:
    cout<<"die";
    die die die die die die die die die die die die die die die die die die die die die die die die;

    mommasboy:
    cout<<"mommy?"
    mommy mommy mommy mommy mommy mommy mommy mommy mommy mommy mommy mommy;

    plus does this string comparison work anyways?

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > plus does this string comparison work anyways?

    No.

    And use functions.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Unregistered
    I am extremely thankful to my computer science teacher for not even bothering to grade work with goto statements. goto statements are bad form, and will lead to bad code, and worse, will preclude you from ever getting a serious computer programming job.

    Break the habit ASAP, because if you get used to using goto controls, it will just be harder to get into proper structured programming.
    I have been corrected for believing this...

    For beginners, I agree that its usage can lead to bad code and at that point, time spent strengthening your code structuring skills are more important....

    goto does have its attributes in that you can change the flow of the code without the overhead of a condition.......it can be handy for breaking out of deep nested loops in one statment....

    I suppose the fact that this statement has survived the test of time and various standard commissions is proof that it has its uses.....

  11. #11
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    I use goto in small condition cases and ONLY when I HAVE FULL CONTROL or i'm feeling lazy to code.

    cout<<"enter month : ";
    m1:
    cin>>mm;
    if(mm>12)
    goto m1;

    BUT if you can avoid the use of goto it its better.

    it leads never ending loops, program crashes, system crashes, angry senior programmers, nuclear attack on your country etc etc..

    for the particuar case of repeating the whole program use do-while, its better IMHO than while in this case.
    -

  12. #12
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > its better IMHO than while in this case.

    Wha...?

    Code:
    cout<<"enter month : "; 
    m1: 
    cin>>mm; 
    if(mm>12) 
    goto m1;
    Code:
    cout<<"enter month : ";
    do
       cin>>mm;
    while(mm>12);

  13. #13
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    I knew that somebody will get

    what i meant by "do-while being better IMHO" is the case he 'ESPARANO' asked

    Umm, ok, that works for small loops but what if I need to do this:

    A:
    blah();
    blah();
    blah();
    if (something==something)
    goto B;
    else
    goto A;
    B:
    blah_blah();
    goto A;
    how would i make a loop to do that?

    ans somebody 'UNREGISTERED' replied use while
    i prefer using DO-WHILE.

    -

  14. #14
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    IMHO, if you can't program without goto's, then you can't program. Not to be harsh, but procedural programming should be structured. goto's violate structured programming. I should be able to look at a flowchart of your program and be able to trace a line from beginning to end without having to lift my pencil.

    goto's are probably the lamest thing on the planet. continue's are up in the top 10.

  15. #15
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Originally posted by ihsir

    ans somebody 'UNREGISTERED' replied use while
    i prefer using DO-WHILE.
    while() and do-while() shouldn't be used interchangably. do-while() is usually used for menu systems b/c the code inside the block will be executed AT-LEAST once. A block in a while() loop is never guaranteed to execute.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typecasting -- Good or Bad
    By Roaring_Tiger in forum C Programming
    Replies: 6
    Last Post: 01-21-2006, 04:41 PM
  2. 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
  3. How do i use Goto?
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 01-06-2002, 10:38 AM
  4. but why not GOTO?
    By LonelyPlanetWanderer in forum C Programming
    Replies: 13
    Last Post: 12-24-2001, 11:46 AM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM