Jump to content
  • Advertisement
Sign in to follow this  
Thekill473

how can I restart a loop: c++

This topic is 2541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. One thing i can't figure out is how to restart(not break) a loop while i'm in the middle of it. This is just a solution to one of my problems because i couldn't get 2 SDL events working in my main loop. Note the i cant just do a while loop aroudn the whole thing because i only need to separate parts of the code with a restart.
thanks for reading.

Share this post


Link to post
Share on other sites
Advertisement
I feel I am not doing you a favore by providing you with an answer, but:

for ( int i = 0; i < 40000; ++i ) {
if ( restart ) {
i = -1; // Goes to 0 after the ++i.
}
}



This is extremely clunky and in my entire life I have never needed to use this.
There is always a better way, and you should be looking at why you need to use this type of hack to fix whatever problem you are having.
Assuming this is what you meant by "restarting a loop".


L. Spiro

Share this post


Link to post
Share on other sites
Factoring the code into separate functions/data structures should allow you to "restart" the loop by placing it inside another loop. Can you show us the code?

Share this post


Link to post
Share on other sites
I would probably do it with a GOTO statement with a nice clear label like 'goto RESTART_HERE;' (and the label RESTART_HERE: above the FOR statement of course)
which cant be any less obscure/mystifying to what its doing than most other ways of coding it.

And since the whole context inside the loop is probably blown (OP asking to restart the loop without any other qualifying specification)
then the cries of 'it will screw up the compilers optimization for the statement blocks' likely wont be true in this case.



Oh and depending on whats inside the loop (like lots of nesting fun) and how entangled the restart logic case is then multiloop exiting logic
can get very very ugly and complicating the coding for the 'goto-less' solutions (flag testing on every nested loop, exit coding if statements follow the loop at each level,etc...)

the 'i = -1;' suggested above might have to be followed with a 'continue;' statement (for more than the trivial example given)


------------


You may want to add a retry limiting test if the 'SDL Event' keeps failing at the point that you decide to 'restart' (and possibly cleanup if other context data was setup/allocated/constructed inside the loop -- to bring the data state back to the same it was before the first loop was run)

Share this post


Link to post
Share on other sites

Hi. One thing i can't figure out is how to restart(not break) a loop while i'm in the middle of it. This is just a solution to one of my problems because i couldn't get 2 SDL events working in my main loop. Note the i cant just do a while loop aroudn the whole thing because i only need to separate parts of the code with a restart.
thanks for reading.


try


void common_code()
while()
{
for(...)
{
common_code()
unique_code()
}
for(...)
{
common_code()
}
}

or any other nested loop.

Don't use GOTOs.

Share this post


Link to post
Share on other sites
Also doable with a while loop.


int indexCounter= 0;
while(indexCounter < maxIndex)
{
//SomeCode

if(exitConditionMet)
break;

++indexCounter ;
if(restartConditionMet)
indexCounter = 0;
}


It's pretty much the same as a for loop converted to a while loop, but I know that I find it more readable. Probably not the case for everybody, but for some reason when for loops have their loop counters messed with I always find the code to be harder to understand than if I turn it into a while loop. Just easier for me to grasp more quickly when I'm reading code.

This is probably in part because most people give nondescript variable names to their for loop counters ('i','j','k'...)

Share this post


Link to post
Share on other sites

Hi. One thing i can't figure out is how to restart(not break) a loop while i'm in the middle of it. This is just a solution to one of my problems because i couldn't get 2 SDL events working in my main loop. Note the i cant just do a while loop aroudn the whole thing because i only need to separate parts of the code with a restart.
thanks for reading.


What does your loop look like now ?

Something like ....

for (int i=0;i<whatever;++i) {
firstPartofLoop;
if(someCondition) {
continue; //continue skips the rest of the loop and starts again from the top, if you want to start over completely you have to set i to -1 before continue (the increment will be done afterwards pushing it to 0)
}
restofLoop;
}


should work, (Allthough in my example you could just as easily use else for the second part of the loop)

Share this post


Link to post
Share on other sites

This is extremely clunky and in my entire life I have never needed to use this.
There is always a better way, and you should be looking at why you need to use this type of hack to fix whatever problem you are having.


This can't be emphasised enough. 99 times out of 100, if you ever find yourself needing some kind of evil construct that's not well supported, the possibility is that you're either missing a more obvious/simple way of handling it, or you're trying to threat a symptom rather than the underlying problem itself.

Share this post


Link to post
Share on other sites

I would probably do it with a GOTO statement with a nice clear label like 'goto RESTART_HERE;' (and the label RESTART_HERE: above the FOR statement of course)
which cant be any less obscure/mystifying to what its doing than most other ways of coding it.

And since the whole context inside the loop is probably blown (OP asking to restart the loop without any other qualifying specification)
then the cries of 'it will screw up the compilers optimization for the statement blocks' likely wont be true in this case.



Oh and depending on whats inside the loop (like lots of nesting fun) and how entangled the restart logic case is then multiloop exiting logic
can get very very ugly and complicating the coding for the 'goto-less' solutions (flag testing on every nested loop, exit coding if statements follow the loop at each level,etc...)

the 'i = -1;' suggested above might have to be followed with a 'continue;' statement (for more than the trivial example given)




------------


You may want to add a retry limiting test if the 'SDL Event' keeps failing at the point that you decide to 'restart' (and possibly cleanup if other context data was setup/allocated/constructed inside the loop -- to bring the data state back to the same it was before the first loop was run)


Ok... someone smack this guy! :)

When I read the thread title I was sure I was going to see at least once suggestion to use a goto and I was not disappointed!

If I read the OP correctly, what you want to do is short circuit the loop process right? As in if X condition occurs, stop executing and go to the next iteration of the loop? If that is the case Simon's suggestion is bang on, as that is exactly what "continue" does. That said, when making a post like this, it helps greatly if you include the language you are working in, although I don't know if a language with SDL bindings doesn't implement the continue key word.

Share this post


Link to post
Share on other sites
..stay away from goto statements. Ive never had to use it, ever. It enforces the idea or the disease of spaghetti code to the programmers mind in my opinion.


If you've done your homework, you should already know how to restart the loop hundreds of different ways.
Its also bad to respond to a question like this with code. That teaches nothing but the will to ask more questions later on possibly about the same thing. Learn it, get it out of the way so you can do the same, possibly in a more complex way later. With never having to ask a question about it, in turn wasting your own time.

http://cplusplus.com/doc/tutorial/ Using creativity, you can answer your question yourself

I also teach it online, id be happy to spread my programming religion ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!