Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2009
Member
 
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!
Reply With Quote
  #2  
Old 19-02-2009
Member
 
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.
Reply With Quote
  #3  
Old 19-02-2009
Member
 
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.
Reply With Quote
  #4  
Old 19-02-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "To use goto statement or not while coding the program?"
Thread Thread Starter Forum Replies Last Post
Java program to use enum in switch statement. MADGE25 Software Development 5 01-02-2010 06:12 PM
How to use the do-while loop statement in java program? KALIDA Software Development 5 27-01-2010 07:53 PM
How to use the break statement in java program? Madaleno Software Development 4 23-01-2010 09:27 PM
How to use the continue statement in java program? Luz Software Development 4 23-01-2010 08:40 PM
Java - goto or continue Miles Runner Software Development 5 16-01-2010 10:47 AM


All times are GMT +5.5. The time now is 01:13 AM.