Results 1 to 4 of 4

Thread: To use goto statement or not while coding the program?

  1. #1
    Join Date
    Feb 2009
    Posts
    7

    To use goto statement or not while coding the program?

    Hi,

    I want to know why people say that we should not use goto statement?

    What are the problems with it I find it easy to turn the flow of my program with just a single line in C?

    Please explain!

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: To use goto statement or not while coding the program?

    I do know the "good uses" of 'goto'. However, I have never found myself needing to use it. I have found, however, that usually the alternative clean solution to using goto is better encapsulation. The thing is that the "good use" of 'goto' often deals with resource management/freeing, and I have found that encapsulation is a much safer and cleaner way for resource handling, and a good implementation actually makes the code better, safer and more reusable.

    Just as a very small example to visualize what I mean: When you handle C-style "strings" (ie. dynamically allocated char arrays) in a function, you constantly need to be very careful to free everything you have allocated at each exit point of the function.
    Because it's tedious to write all the same freeing calls at each exit point, 'goto' is sometimes used to jump to the end of the function where everything is freed. However, compare that to using std::string in C++: You don't have to worry about freeing them because they will be automatically freed when the function is exited. You don't need to use 'goto'. At the same time the function becomes cleaner, easier to read and, most importantly, *safer*.

    And no, I'm not saying that's the only "good use" of 'goto'. I just wrote one example of what I'm talking about.

  3. #3
    Join Date
    May 2008
    Posts
    115

    Re: To use goto statement or not while coding the program?

    However, it's generally accepted that using goto every now and then to brake out of multiple nested loops is better than creating flags and adding checks to the conditions.

    If you're a beginner, though, it's better if you avoid it. At least until you create some criteria of when to use it and when not to use it.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: To use goto statement or not while coding the program?

    I seem to recall being called a horrible, talentless coder (or something similar) when I argued that there might be a reason to use gotos. Specifically, case statements used for state machine code, but whatever.

    Of course, the fact that calling foo(x) is, for all practical purposes, a GOTO (really a GOSUB) is just a bit too 'deep' for some people.

    http://www.cprogramming.com/tutorial/goto.html

Similar Threads

  1. Java program to use enum in switch statement.
    By MADGE25 in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 06:12 PM
  2. How to use the do-while loop statement in java program?
    By KALIDA in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 07:53 PM
  3. How to use the break statement in java program?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 09:27 PM
  4. How to use the continue statement in java program?
    By Luz in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 08:40 PM
  5. Java - goto or continue
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 10:47 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,916,179.18284 seconds with 17 queries