On Dienstag, 15. April 2003 14:34, Jerry Feldman wrote:
I've read all the other responses. In my 20 years of using the C language, I have never had the occasion to use goto in my own code.
Same here (except that I've been programming C for just about 15 years ;-) ).
HOWEVER, there are cases when you might be very deep in a nesting situation where it may be appropriate.
Well, kind of...
Derek's example, while(looping && condition)
works well, but adds another test in the loop. You might have something like: case 'x': /* falsify the condition */ continue;
In a recent porting project of complex code involving multi-level locking there was a case where a goto was used such that all code would exit through a common unlocking point.
My criteria has always been to keep code an simple and straighforward as possible. If an occasional goto is needed, then by all means use it.
Right. Not using "goto" at all should not be taken religiously - it's a good guide line, no more. BUT it can in most cases easily be avoided by a premature "return" or - like pointed out above - "continue", both of which I personally prefer in that given situation. I'd try to avoid "break" except inside a "switch" statement, though, because even if the code's author knows for sure how far outside this "break" will take him, the next person to maintain that code may not - which makes this code hard to maintain. Anyway, all of those possibilities of course merely hide the fact that the resulting machine code will most certainly be some kind of "jump" or "branch" statement - which is the machine code equivalent of "goto". ;-) Bottom line: Use your common sense. Don't be religious. CU -- Stefan Hundhammer <sh@suse.de> Penguin by conviction. YaST2 Development SuSE Linux AG Nuernberg, Germany