"goto" question

This is a discussion on "goto" question within the C++ Programming forums, part of the General Programming Boards category; Note that since goto is so rarely used, compiler optimizations will rarely include mechanisms that work with goto....

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Note that since goto is so rarely used, compiler optimizations will rarely include mechanisms that work with goto.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I've noticed that compilers always seem to use that format. a more efficient way would be to restructure the loop by putting the comparison at the bottom, followed by a conditional jump to the top. before entering the loop initially there would be a jump to the comparison.
    I've noticed that too -- if you use while(1) instead of for(;;), and if compiler optimisations are turned off.
    Code:
    gcc -W -Wall -ansi -pedantic -O2 -o program program.c
    You can use -s too if you want to strip the executable.

    Search the board for "goto" and you'll find about 20 pages of results.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    While it's true that goto i inconvenient for writing programs efficiently that need to work correctly and be readable, it is not bad for beginners learning how to think and how to solve problems.
    In my opinion, that's the worst advice I've ever seen posted on this forum.

    I'm just a newbie in c++
    Then forget you ever heard about goto, and certainly don't ever use it in one of your programs. Its use should be reserved for experienced programmers who know what they are doing. If you want to see what happens when a beginner uses goto in a program, check out this program:

    hexadecimal
    Last edited by 7stud; 02-13-2006 at 09:32 PM.

  4. #19
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by 7stud
    In my opinion, that's the worst advice I've ever seen posted on this forum.
    Would you mind explaining why?

  5. #20
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    dont just let a few examples that u seen above dictate your inclination towards the goto,and your proposition that" everyone hates goto" is ofcouse false,cuz I LIKE GOTO'S.
    i started with goto's ,goto's r easy to implement and visualize,once u learned a fair bit,and u r experienced and u know whats going on,how is the data and control flowing through your program u can think about optimizations ,u can think about removing the goto's.
    For a complete beginner i suggest," u cant learn to program like an expert in a day,so try to keep things simple,do what u find easy" as u move on towards more complicated code u will find it will be harder to track multiple goto's in your code,u wont be able to read your program in a top down style,u have to keep following the goto's ,possibly one goto leads to another and god forbid if that one leads to another,see u r already lost.
    AND COME ON r u thinkin about objects?



    and please completely ignore kitten,this is not the time to be thinkin about compiler optimizations,u say u r a newbie,U R JUST A LIAR,u r talkin assembly,u r talkin programmin without goto's ,u r talkin about supper efficient code,u r talkin about gettin the job done in under 1 nano second,ARNT U AN EXPERT ALREADY?


    __________________________________________________ __
    "its not programming if its not object oriented"
    Last edited by qqqqxxxx; 02-13-2006 at 10:42 PM.

  6. #21
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by Rashakil Fol
    Would you mind explaining why?
    Well for one it doesn't teach them how to do function-based or OO-based programming. What looks worse? Sticking everything in main or using functions/OO? Also it will better prepare them for languages without that keyword if they learn not to rely on it.

    Quote Originally Posted by qqqqxxxx
    i started with goto's ,goto's r easy to implement and visualize,once u learned a fair bit,and u r experienced and u know whats going on,how is the data and control flowing through your program u can think about optimizations ,u can think about removing the goto's.
    I seriously hope your coding skill is better than your English because I find it both difficult and painful to read what you've just said.

  7. #22
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    thats what? see how right u r? thats why they dont teach them calculus rite when they r in 2nd grade?
    damn,says one 2 grader ,why r we not being taught the calculus it will only prepare us better ,cuz u know we got to be working with the schrodinger equation sometimes.







    reall? u really hope my coding skills r better than my english skills?
    omg? should i thank u or what?

    and <newbie alert>
    try teh goto's,they r very easy,easier than all the switches,easier than all the conditionals,easier than all the loops,dont waste your time wondering how to replace the goto's that u got(lol see u already got the goto's),think about solving the matter at hand.
    gettin an output . later we all shall gettogether and discuss what we can do to make the code more readable,till then keep your code a secret,dont tell anyone u used them gotos cuz u got em bitc_hin then.</newbie alert>
    Last edited by qqqqxxxx; 02-14-2006 at 05:55 AM.

  8. #23
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Frobozz
    Well for one it doesn't teach them how to do function-based or OO-based programming. What looks worse? Sticking everything in main or using functions/OO? Also it will better prepare them for languages without that keyword if they learn not to rely on it.
    I'm not saying students should be using gotos all throughout college. I'm talking about the early parts of an introductory programming class. There are many students early on who can't figure out that to see how a program works, they have to step through it in their mind. They could understand how for (i = 0; i < s.size(); i++) s[ i ] = toupper(s[ i ]); works, but not what x = 1; for (i = 0; i < 4; i++) { x *= i + x; cout << x << endl; } outputs. It's as if they never noticed that a for loop is executed by stepping through it, one at a time.

    Also, there are some types of programs where the solution is obvious if done with a goto. One great example is the one linked a few posts back. If a budding programmer has to deal with something relatively complicated early on, that's good for his problem solving abilities. Then, when the rules change and a homework assignment says, "You may not use goto," the student has to adapt his thinking towards reworking his usual goto-based solution to something a bit more organized. For example, the write of the program above would have to figure out to use a switch and a next_menu variable inside a while loop. But when solving the problem, he would have an open shot at getting something he can work with on the table, instead of restricting his angles of attack to those which conform to some combination of ifs, fors, and whiles.

    One disadvantage of C and C++ as teaching languages is that they don't really let you do anything interesting with the language. A student being taught with Scheme would have even more angles of attack than a C or C++ learner, at the end of his first semester or two.

    The job of early introductory classes is to teach kids problem solving, not program code writing.

  9. #24
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    The following is an example, I am not equating people to dogs but it is a good analogy.

    When you first get a puppy, you immediately start training it if you don't want to have troubles later. You teach it to do its business outside, sit, laydown, to not beg(this is one that must be done early).

    The reason you teach these things at a young age is it develops habbits later in life. When the dog hears sit, it sits, when it needs outside it lets you know. When you are eating, it wont get a foot away from you and drool on your shoes, it will go lay down and ignore the urge to beg.

    Now in programming, you are learning a whole new way of thinking. Most of us (atleast I know I do) when I am coding something I have done something like before, I can generally do a couple hundred lines without having to really stop and think about how to do certain things. I can just use my for and do/whiles and all those other nice loops, along with my functions, classes, and OO related coding. If I had learned to use goto's when I eas first learning I would probably just use them as I went now a days. It is habbits that one must worry about when programming. You develop a habbit and then when ever you encounter a like situation, you fall back on habbit, whether it be good or bad.

    Learn to program right the first time. In C++ we don't need gotos because we have functions and many other tools. In a strictly precedule language I can see their use, but even then, a nice while will do you wonders.

    One disadvantage of C and C++ as teaching languages is that they don't really let you do anything interesting with the language. A student being taught with Scheme would have even more angles of attack than a C or C++ learner, at the end of his first semester or two.

    The job of early introductory classes is to teach kids problem solving, not program code writing.
    C++ is a great langauge to learn with, you get a strict syntax, OO ideas, it is the language most games are written in (since a lot of novice programmers are looking to make games).

    Secondly, the job of those early introductory classes, is problem solving, and using the tools properly to solve it. Generally speaking, it is also a class on program code writing, because if you are taking classes, the first couple classes teach the language that you are going to be using the next 3 years at that school while you go though the computer science track.
    Last edited by Wraithan; 02-14-2006 at 09:57 AM. Reason: I decided I had more to say.

  10. #25
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Try to follow this pattern with functions:

    One entry. One exit.

    Code:
    bool done = false;
    
    while(a && !done)
    {	
    	while(b && !done)
    	{
    		while(c && !done)
    		{
    			if(x)	done = true;
    		}
    	}
    }
    First of all I never nest loops that deep and second I can tell exactly what is taking place in this code. The goto is not needed in this instance, nor any, if you ask me.

  11. #26
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Habits are the last thing you should be encouraging. For starters, you can't enter a new family of programming languages if you're bogged into a habit. The fact that people successfully go from knowing only C++ to learning radically different languages, such as Haskell, is proof that habits do not stick for life. Programmers, and other humans, are not dogs.

    Humans that are incapable of breaking their habits are the pathetic ones who can't get past the 'weirdness' of the indentation of Python, the parentheses of LISP, or the postfix/infix syntax of C++, and actually learn something new. Also, if you're using FORTH, or [insert interesting language of choice], the 'right' way of C++ is impossible, or wrong.

    There is a large number of people who first programmed on the TI-83 and TI-85, where goto is unescapable, who successfully use other languages the 'right' way today.

  12. #27
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    This is a fruitless argument. No one, nor myself will convience you. Once you have to be in the workplace debugging both your own code and code of those to come before you and work beside you. Then you will see the value of object oriented programming. Learning that from the begining helps because it is a way of thinking.

    I program in several languages all day, I debug in even more languages. I had some bad habbits from when I started programming that went away over time, but breaking those habbits was hard, and if I could, I would go back and learn the languages properly.

    As a side note, even if an instructer says something is ok to use in a program, that doesn't mean it really is. Such as the use of void main, another habbit by some, a lot like gotos, other than gotos haven't been officially depreciated, just shuned and put out of use by programmers who have more than just immediate use for a program.

  13. #28
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    its a small wonder,why people think they have made mistakes,developed habits that were hard to break,but did u think about what got u programming that way b4,what got the habit inside you?
    its too easy to retrospect and say a 1000 things that u could have done right,but u didnt.
    iF you would have got everything right the first time u would not have been the programmer u r now.
    actually goto's and loops are nothin different ,they are all the same,but loops are a bit rigid ,they dont allow u the flexibility.
    goto's are simpler than the loops,whether u r doing the loops or u r doing the goto's you are doing only the goto's,thats just abstraction for u.if u follow a particular naming style,programming style,when using the goto's i cant see why u will ever get lost.

  14. #29
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by qqqqxxxx
    when using the goto's i cant see why u will ever get lost.
    The danger of gotos is which way they jump and how often they are used. When used less than 1% of the time, confusion is minimal. When they are used to jump to more than one place in a hunk of code, confusion with respect to program flow can increase.

    Following code that moves forward and backward against the style of the language adds to confusion. Avoiding that, goto can do exactly what you want more efficiently and clearly than other methods. Used poorly, it does lead to difficult to maintain spaghetti code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #30
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by qqqqxxxx
    if u follow a particular naming style,programming style,when using the goto's i cant see why u will ever get lost.
    There is already a particular naming style and programming style when using gotos; they're called for loops, while loops, and braces. There are all sorts of reasons why goto is inconvenient for writing and editing code. For starters, lack of meaningful indentation. Inability to have variables only live within a certain scope. The extra typing and extra reading. The inability to easily modify the program. For example, in that post linked several posts back, a programmer would have a much harder time adding a new option to the program than he/she would if it had initially used a while loop with an extra flag variable lying around, in which case adding a new option would be trivial.
    Last edited by Rashakil Fol; 02-15-2006 at 04:50 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21