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.
how can I restart a loop: c++
I feel I am not doing you a favore by providing you with an answer, but:
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
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
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?
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)
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)
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.
Also doable with a while loop.
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'...)
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'...)
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)
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.
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.
..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 ;)
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 ;)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement