Thread: GOTO and Labels

  1. #1
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    GOTO and Labels

    was wondering is it really bad programming to use GOTO and Labels while i c it very usefull in speciefic situations..........
    ?????????????????????????????????????????????????? ???????
    Programming is a high logical enjoyable art for both programer and user !!

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    not necessarily, goto's should be used sparingly but are far to powerful a tool to be completely ignored!
    they are as you say quite useful in situations, where any other method would be excessivly painful, and unecessarily long.

    they are not the evil that is claimed if used properly where they are needed.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    I think their evilness is often over-emphasised so that new programmers don't get into bad habits. Having said that there aren't any situations where they are really necessary, but they can be useful in optimising something like tail recursion (if your compilers optimiser isn't up to the job) or a quick jump out of a nested loop.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >Having said that there aren't any situations where they are really necessary,

    the how would you solve this little delima more eloquently, though simplified can come up quite often

    Pseudocode:

    Code:
    // stuff...
    
    for(val,value; val != whatever; val updated)
    {
        dostuff();
        for(;value != whateverelse;value updated)
       {
            switch(value)
            {
                case 1:
                    dosomething();
                break;
                case 2:
                    updatesomething();
                    // uh oh special exit condition reached!!!! we need to get back to the main loop
                    goto MAIN_LOOP;
                break;
                case 3:
                    dosomething();
                break;
            }
        }
        MAIN_LOOP:
        dosomethingwithvalue(value);
    }
    
    // more stuff...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    You could set a flag. I didn't say anything about what people find more eloquent and actually mentioned that they could be useful in jumping out of nested loops for speed; but the fact remains that they're not necessary. There's always another way to code something in C++ where they're not required.

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >There's always another way to code something in C++ where they're not required.

    this is not really a "technically" correct or incorrect statement the use of a goto is especially in a far more complicate version i could write up neccessary in this situation since virtually any other method would be a complete waste of resources, time, code length, complexity, and effort, and in my !Opinion! for lack of a more precise and better word "incorrect".
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    // stuff...
    
    for(val,value; val != whatever; val updated)
    {
        dostuff();
        for(;value != whateverelse;value updated)
        {
            switch(value)
            {
                case 1:
                    dosomething();
                    break;
                case 2:
                    updatesomething();
                    // uh oh special exit condition reached!!!! we need to get back to the main loop
                    break;
                case 3:
                    dosomething();
                    break;
            }
            if (value == 2)
               break;
        }
        MAIN_LOOP:
        dosomethingwithvalue(value);
    }
    
    // more stuff...
    Last edited by swoopy; 07-01-2002 at 03:20 PM.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    It is a technically correct statement. It doesn't matter how much more hassle it is because you have chosen to structure your code in a particular manner, it is still possible to do it without gotos (and it may possible to make it alot less work than you think with a bit of restructuring). It has nothing to do with opinion.

  9. #9
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    nice snoopy but, like i said this i much simplified version, and just an example for the purpose of discussion.

    and this is as stated opinion of eloquence... and in my honest opinion thats ugly, especially if you had a dozen case's which required this exit fro mthe sub loop...

    so stop being a smarty!! heheh.

    >It doesn't matter how much more hassle it is because you have chosen to structure your code in a particular manner<

    how else could i structure it?
    It wouldn't be worth the trouble to complete restruture the code just to prevent the use of a goto! whats the point!!

    >and it may possible to make it alot less work than you think with a bit of restructuring

    this is in some cases true, but not all.

    > It has nothing to do with opinion.

    it has everything to do with opinion!!
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >how else could i structure it?

    It's hard to say with the contrived example you've given as I've no way of knowing what you're attempting to achieve. A quick hack alternative might look something like -

    Code:
    for(val,value; val != whatever; val updated)
    {
        dostuff();
        for(;value != whateverelse;value updated)
       {
    	   if(value==1){
                dosomething();
                continue;
    	   }
    	
    	   if(value==2){
                updatesomething();
                    if(special condition)
    					break;
    				else
    					continue;
    	   }
            
    
    	   if(value==3){
                 dosomething();
                continue;
            }
        }
       
        dosomethingwithvalue(value);
    }

    >it has everything to do with opinion!!

    What does? I wasn't refering to which one looked nicer or took less work, but to your assertion that it was not technically correct that it's possible to do it without gotos, which is false.

  11. #11
    Unregistered
    Guest
    >What does?

    THE USE OF GOTO'S!!

    > technically correct that it's possible to do it without gotos, which is false.

    reread the post i sent it before i finished what i was saying...

    it was meant as "this is not really a "technically" correct or incorrect" but to say that it is always possible does not mean its always better... maybe technically was not the best term but, allow me to clarify my meaning.

    it is not technically correct to do something total unneccessary and complex to avoid the use of gotos,

    in my (as stated)OPINION!, it is not technically correct, please read it in the context it was stated in.

  12. #12
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    thank you guys for ur response.... after all I would like to tell you I agree totally with (Unregistered) its legal statement, however its real bad habit , and programmers should avoid it(even if this ll cost longer code)
    one of the most experienced programmer(the author of Teach yourself C++ in 21 days) mentioned that he needed to use (GOTO ) once in 10 years of programming with C++ !

    thanks you all again
    Programming is a high logical enjoyable art for both programer and user !!

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    if the author of your book thinks it takes just 21 days to learn c++
    then u should'nt listen even listen to the guy

  14. #14
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    hehehe........I agree with you defenitly, anyone who says that learning c++ needs 21 days , we shouldnt listen to him, but in fact the man didnt say that. He just offers a nice introduction to the c++ away from the dry books, in fact its very good book as a beginning. and at the last chapter he adviced the readers to go ahead for more comlicated books just likec " effrctive c++" and "c++ the programming language"
    Programming is a high logical enjoyable art for both programer and user !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm New...=(
    By thisisurlife08 in forum C++ Programming
    Replies: 46
    Last Post: 05-13-2008, 04:34 PM
  2. A peculiar problem in displaying a file.
    By cheemais in forum C Programming
    Replies: 22
    Last Post: 10-03-2007, 10:25 AM
  3. Labels for premature function exit + cleanup
    By cboard_member in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2006, 05:02 PM
  4. Need help with goto in c++
    By darealnash in forum C++ Programming
    Replies: 23
    Last Post: 07-07-2004, 05:17 AM
  5. #define goto _asm JMP
    By FillYourBrain in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2003, 09:23 AM