how can I restart a loop: c++

Started by
25 comments, last by Thekill473 12 years, 5 months ago
Trivial examples dont really show whats involved with real world code (so nice and clear when all parts of the more complex but 'pure' or 'correct' control pattern
are 3 lines apart, but not quite that clear when there are 5 pages of other code between the different bits obscuring the parts)

I thought the OP was talking about 'restarting the loop' entirely (not just going back up to the top of one iteration)

If a call (SDL) is failing then a local retry loop (definitely with a limit and then a failure exit path) would be called for.
I still might the use another GOTO for the exception path to jump down to a cleanup phase (and again likely bypassing alot of other code and avoiding alot of extra special exit handling logic).

Ive been programming (professionally) for 30 years (has it really been that long) and have read/scanned millions of lines of code and fixed alot of bugs caused by other programmers screwing up their control flows. Its much easier to interpret/debug the code when its shorter/simpler/clearer (ie- comments do help and also goto LABELS that mean something).
So many NON-trivial cases where complexities (shear length, multitudes of nestings/logic paths, etc..) turned the 'pure' structured programming into a horrendous mess.

The hatred for the noble 'goto' statement probably originated back in the old Fortran days when gotos only jumped to line/label Numbers and comments were minimalized (and those Fortrans often could only do single THEN statement -- very often a goto jump). An absolutism developed due to the pain of those restrictions leading to academics preaching their goto-less ideology frequently with little experience of the real world complexity of many programs. They attempted to banish something so simple when their designs very often required 'three lefts to make a right' instead.


Anyway, Ive used more than a few gotos -- most usually for exception processing and some more to eliminate alot of ugly deep exiting/retry logics, always using
clear labels and good comments explaining the divergence of the control flow. I use Breaks and Continues when they are simpler (again with comments since they are often not obvious of themselves where the control transfers to.

I vaguely recall that someone proposed C/C++ having Continue/Breaks to be able to specify which (named) nested loop/block they processed (ie- breaking out/jumping several loop nestings) which could eliminate/cleanup many common exit coding situations.
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
Advertisement
Meh. I have yet to use a goto statement in my code, but to be honest I'd rather see someone use a goto statement rather than a do { } while (0) with continues/breaks. But even more so, I'd rather see someone just wrap the bloody thing in a function and use an early return than a goto statement or a do { } while (0) with continues/breaks.

My opinion is just to make everything as atomic, short, simple, and clear as possible. Don't sacrifice common sense for fear of using a tool of the language, but also don't abuse any tool of the language either. This involves gotos and everything else there is. No sense in starting a holy war.

But yeah, I'd say to the beginner learn what a goto is, and then try to never use it (I didn't say never use it; just try not to). If your code is riddled with gotos you're probably doing it wrong.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
All this talk of whether to GOTO or not is quite theoretical. Unless the OP shows us the source we can't really divine which would be the messier solution.

That said, I rarely think of "restarting" a loop when I'm coding, so I'm guessing the OP has maybe an atypical algorithm, or is handling errors in an unusual manner. My gut reaction is that using a goto will probably make the situation worse. To add my own terrible metaphor to the mix, gotos are like intense radiation. You should avoid exposure in general, but it can occasionally be useful for killing off cancerous growths (i.e. spaghetti code).

I'd like to see the OP's code, because "i couldn't get 2 SDL events working in my main loop" doesn't sound like something that should be a problem, and the "solution" of "restarting" a loop is suspicious.
I have yet to find a use for goto that I was satisfied with. Anywhere.

Regardless, it's not even clear what the OP means by "restart".

All this talk of whether to GOTO or not is quite theoretical. Unless the OP shows us the source we can't really divine which would be the messier solution.

That said, I rarely think of "restarting" a loop when I'm coding, so I'm guessing the OP has maybe an atypical algorithm, or is handling errors in an unusual manner. My gut reaction is that using a goto will probably make the situation worse. To add my own terrible metaphor to the mix, gotos are like intense radiation. You should avoid exposure in general, but it can occasionally be useful for killing off cancerous growths (i.e. spaghetti code).

I'd like to see the OP's code, because "i couldn't get 2 SDL events working in my main loop" doesn't sound like something that should be a problem, and the "solution" of "restarting" a loop is suspicious.


This.

This whole question really smells like a bowl full of fish guts (since we're making outlandish analogies :) ). I'd really like to know what the OP is actually trying to do, since it sounds like he/she is trying to band-aid some kind of massive, leaking head wound.

[quote name='rip-off' timestamp='1320313861' post='4880062']
All this talk of whether to GOTO or not is quite theoretical. Unless the OP shows us the source we can't really divine which would be the messier solution.

That said, I rarely think of "restarting" a loop when I'm coding, so I'm guessing the OP has maybe an atypical algorithm, or is handling errors in an unusual manner. My gut reaction is that using a goto will probably make the situation worse. To add my own terrible metaphor to the mix, gotos are like intense radiation. You should avoid exposure in general, but it can occasionally be useful for killing off cancerous growths (i.e. spaghetti code).

I'd like to see the OP's code, because "i couldn't get 2 SDL events working in my main loop" doesn't sound like something that should be a problem, and the "solution" of "restarting" a loop is suspicious.


This.

This whole question really smells like a bowl full of fish guts (since we're making outlandish analogies :) ). I'd really like to know what the OP is actually trying to do, since it sounds like he/she is trying to band-aid some kind of massive, leaking head wound.
[/quote]


I still put my money on he wanted the equivalent of continue. That said, since everyone is talking in this thread but the OP, we may never know. :)


EDIT: Actually in re-reading, I think it may actually be a control flow problem, where he wants to handle two events in the same iteration of the loop, but instead of doing that, is trying to solve it by serializing the processing of events and starting the loop again.
Thanks everybody for all your help. Ive merged alot of of your ideas together to make a solution that fits my project best.

This topic is closed to new replies.

Advertisement