Thread: goto

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    goto

    How do you use goto statements? The book I'm using doesn't cover it at all.

    Please don't reply to this thread if you're going to say: "You should never use goto statements in your code. There are always better solutions using loops than to use gotos."

    I remember a while back hearing something about a label...

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    A Little example will probably help you best:

    Code:
    // This Code Comes Straight From:
    // Sams Teach Yourself C++ in 21 Days 3rd Edition
    // Jesse Liberty
    
    #include <iostream.h>
    
    int main()
    {
    	int counter = 0;
    loop:	counter++;
    	if(counter < 5)
    		goto loop;
    
    	return 0;
    }
    I have to say it though. Don;t use em. They are junk... Nothing but junk... Like little pieces of chewing gum under a chair it is just something that is C++. Nobody wants it there, nobody likes it there and nobody understands why they put it there.....

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Isn't there an ingenious way to use goto statements? Wasn't an operating system programmed with a couple of goto statements in the code? Linux I believe...

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I know you don't want this:

    but since DirX already posted an example.

    goto is undesired. it leaves to often confusing and sloppy code.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    You have a base of truth there. Sometimes goto statements are the only way to go (like there are those people who like to take the chewing gum from under chairs and stick it in ther mouths again ). But generally C++ leaves you with other options. And on that linux comment, Big projects will always get messy and people will always make stupid mistakes... Goto statements are just messy as hell and make the code terrible to read...

  6. #6
    Usually goto statements can be replaced by some type of loop (for, while, do..while).

  7. #7
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    I'd personally rather find a way to do it without 'goto' just for the simplicity of reading the code. However, it wont make your code unstable, and it isn't unprofessional either. You can use whatever method that you want.
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

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