• 12
• 11
• 9
• 10
• 13

Searching loop

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

Recommended Posts

for (int i = 0;i<1;++i){ if (condition){ i=0; //keep searching } else{} } This I thought was working but it always runs once for me. THe code im using might be doing it.. but really I couldnt see how yet. I tried this and it crashed.. for (int i = 0;i<10;++i){ if (condition){ i=0; } else{i=10;} }

Share on other sites
Quote:
 Original post by VprMatrix89for (int i = 0;i<1;++i){if (condition){i=0; //keep searching}else{}}This I thought was working but it always runs once for me. THe code im using might be doing it.. but really I couldnt see how yet.I tried this and it crashed..for (int i = 0;i<10;++i){if (condition){i=0; }else{i=10;}}

The last part of the for() statement (++i in this case) is executed after one loop iteration. That means when condition is true, the loop increments I and then goes back to the start. You'd need to do i=-1, so it gets incremented to 0. However, the second example shouldn't cause a crash. What exactly happens? Access violation? What exact error?

Although this looks a bit ugly and could probably be done more neatly. What exactly are you trying to do?

EDIT: The second loop will execute 10 times if condition succeeds on the first loop, that might cause your crash, depending on what exactly condition is. Also, problems like this are extremely simple to solve using the debugger. Stepping through the code would see i getting set to 0, then incremented at the end of the loop to 1, then the loop would run again.

Share on other sites

int i;for (i = 0; i < 10; i++){    if (i == 6)    {        break;    }} cout << "The number of iterations performed was " << i;

Also, why not use a while loop? You can still use the break command in while loops if you need.

int x = 10;bool condition = true;while (condition) // terminates if condition is false{    x++;    if (x < 10)    {        condition = false;    {        // do some stuff with the variable x here...}

The above code is simple, and one could do the same thing with break probably (in the above example). Here is another way to search for something:

int i = 0;std::vector<int> v(10); for (i = 0; i < 10; i++){    v = i + 10;} for (i = 0; i < 10; i++){    if (v == 16)    {        break; // stops searching for the number sixteen    }    // otherwise the search is continued for sixteen} cout << "The index for the element in v containing the number 16 is " << i;

You also don't need to do "else{}" in your programs in nothing should happen if a condition isn't met. You'll probably want a for loop like I showed in my last source example, but depending on your situation, a while loop might be better. It just depends on what you are trying to do.

 fixed some crucial mistakes...

[Edited by - MikeTacular on May 24, 2008 8:15:39 AM]

Share on other sites
The reason the second one crashed is because one condition is never met, something stupid i forgot about, made it loop forever.

But still, the first one should be crashing for the same reason. but when I say i=0, apparently its not working? When i put i-=1 it did work for some reason.

Anyways, I need a loop that searches untill a condition is met, and this is the only way I could think of.

I'm going to fix this condition never met now.

EDIT: Question about debugging.. My program will launch and well obviously it freezes. I end program (because I have no choice) and click break or continue all the stuff in the debug screen goes bye-bye, and i gain nothing. Maybe I should change it from full-screen?

Share on other sites
What is the purpose if this heavily undesirable code? Modifying for variable inside for body is a big no-no.

Can't you do something like:
int counter = 0;while (condition) {  //something  counter++;}
or even
for (int i = 0; (i < n) && condition; ++i) {}

Share on other sites
Quote:
 Original post by VprMatrix89But still, the first one should be crashing for the same reason. but when I say i=0, apparently its not working? When i put i-=1 it did work for some reason.
Yes, because the loop variable is incremented at the end of the loop. This:
for(int i=0; i<1; ++i){   if(condition)   {      i=0;   }}

Is the same as:
int i=0;while(i<1){   if(condition)   {      i=0;   }   ++i;}
And will produce the exact same machine code. You're setting i to 0, then it's incremented to 1 and tested to see if it's less than 1, which it isn't, so the loop exits. As MikeTacular pointed out, a while loop would be muvh more readable here.

Quote:
 Original post by VprMatrix89EDIT: Question about debugging.. My program will launch and well obviously it freezes. I end program (because I have no choice) and click break or continue all the stuff in the debug screen goes bye-bye, and i gain nothing. Maybe I should change it from full-screen?
Yes, fullscreen debugging is a pain unless you have multiple monitors or are doing remote debugging.

If the program appears to hang, you can break into it in the debugger, or put a breakpoint somewhere if you think you know somewhere the execution flow is passing through, and then single step through the code to see why it's stuck in a loop.

Share on other sites
edit: I will probably use a while loop although as you say the execution is the exact same still. THank you guys.

I wouldn't say my program is so complex that I need breakpoints, because deleting the loop or its contents usually proves what the problem is. But I can see how they would be useful if the loop was making many outside calls that you couldnt guess at.

@EvilSteve: Single stepping through the problem is slow, perhaps if I get the debugger to work (by changing full-screen setting) I can find exactly what my variables are doing?

Share on other sites
Quote:
 Original post by VprMatrix89At Antheus, the loop termination is not static, the functions inside determine that.. so atleast one of the loops condition variables must be changed.If your suggesting I make another variable I can do that, but then there is no reason to have the for variable at all really.

Antheus had a great idea actually. If you think his method won't work for you, then your original code is flawed even more than you believed, because yours and his do the same thing (functionally wise), but his is written in a clean manner and doesn't produce any errors. You can replace "condition" with anything you want (so long as it's a proper condition) and it will work. You can also create a boolean variable called "condition" if you think you can't write a static condition, and then modify the crap out of the condition variable and then test for its value. If there isn't a reason to have the for-loop-variable, then why aren't you using a while loop?

Also, (this is not meant to be a flame in any way) could you please write in proper English? "Your" and "You're" can change a sentence's meaning, and possessive apostrophes can change everything (and so can the lack thereof).

 Dang, I swear I always post a fraction of a second too late. Never mind VprMatrix89

Share on other sites
Quote:
 Original post by MikeTacularAntheus had a great idea actually. If you think his method won't work for you, then your original code is flawed even more than you believed, because yours and his do the same thing (functionally wise), but his is written in a clean manner and doesn't produce any errors. You can replace "condition" with anything you want (so long as it's a proper condition) and it will work. You can also create a boolean variable called "condition" if you think you can't write a static condition, and then modify the crap out of the condition variable and then test for its value. If there isn't a reason to have the for-loop-variable, then why aren't you using a while loop?Also, (this is not meant to be a flame in any way) could you please write in proper English? "Your" and "You're" can change a sentence's meaning, and possessive apostrophes can change everything (and so can the lack thereof). Dang, I swear I always post a fraction of a second too late. Never mind VprMatrix89

No problem, I tried proper english but I failed that class and I deeply apologize. It's been so long since I have used the while loop that I forgot about it, so I was a bit confused as to what he meant.

Share on other sites
Quote:
 Original post by VprMatrix89@EvilSteve: Single stepping through the problem is slow, perhaps if I get the debugger to work (by changing full-screen setting) I can find exactly what my variables are doing?
How would you single step without the debugger?

You need to use the debugger for problems like this unless you want to spend literally 10 times as long on the problem.