Sign in to follow this  

Searching loop

This topic is 3491 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

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 this post


Link to post
Share on other sites
Quote:
Original post by VprMatrix89
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;}
}


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 this post


Link to post
Share on other sites
You want to use the break command. You can google "C++ break" for more information.

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] = i + 10;
}

for (i = 0; i < 10; i++)
{
if (v[i] == 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.

[edit] fixed some crucial mistakes...

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by VprMatrix89
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.
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 VprMatrix89
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?
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by VprMatrix89
At 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).

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

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular

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).

[edit] 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
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.


I was thinking of using a break at each point in the loop. Ah well.. nevermind.
A bunch of good information has come out of this and it had me thinking about too many ideas at once.

edit: I just got it working now. I am impressed with all the very good explanations all have provided.

edit2: @EvilSteve -- This is minor but it is curious.

for(int i=0; i<1; ++i)
{
if(condition)
{
i=0;
}
}

From what I understand your saying the loop should never be ran under any circumstance, because i is incremented before it is checked, yet the program did run it once, but apparently stopped after that. This isnt very important but someone once told me i++ or ++i effectively made no difference in a for loop, although I don't know if this is true or not

[Edited by - VprMatrix89 on May 24, 2008 9:17:38 AM]

Share this post


Link to post
Share on other sites
You don't listen, do you [wink]?

The increment/decrement/whatever_you_use is done at the end of the loop. Meaning, you start at 0, you loop, then i becomes 1, and 1 is not less than 1, so the loop breaks. If you set i to 0 inside the loop, then at the end of the loop it once again becomes 1, so the loop won't be run through again. That's why setting i to -1 works, because at the end it becomes 0, which is indeed less than 1.

Share this post


Link to post
Share on other sites

"The increment/decrement/whatever_you_use is done at the end of the loop"
Yeah I see now where Steve said this. My fault. Before, I thought that it was done before the loop.

Share this post


Link to post
Share on other sites
Quote:
Original post by VprMatrix89
I was thinking of using a break at each point in the loop. Ah well.. nevermind.

Are you confusing breaks with break points?

Quote:
Original post by VprMatrix89
for(int i=0; i<1; ++i)
{
if(condition)
{
i=0;
}
}

Setting i to 0 inside of the loop cannot have any effect, because there is only one iteration, and i is already 0 in that iteration.

Quote:
Original post by VprMatrix89
because i is incremented before it is checked

I think you're unclear on the meaning of the prefix increment operator.

Share this post


Link to post
Share on other sites
Quote:
Original post by VprMatrix89
This isnt very important but someone once told me i++ or ++i effectively made no difference in a for loop, although I don't know if this is true or not
If i is a primitive type, then it makes no difference because ++i results in the exact same code as i++. However, if i is a more complicated type, like a class (E.g. an iterator), then ++i can be faster because it doesn't need to create a temporary object (++i means "increment i and return the new value", i++ means "increment i and return the old value" - which for classes tends to mean "take a copy of i, increment i, then return the previous copy"). However, the compiler will usually optimise it away anyway. I always prefer ++i just because it's in theory more efficient - particularly if you have a complicated post increment operator that the compiler can't optimise away very well.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
++i means "increment i and return the new value",
i++ means "increment i and return the old value"

Another difference: ++i returns an lvalue (something that has an address), whereas i++ returns an rvalue (just a value). So you could write
++i = 5;

but you could not write
i++ = 5;

because you can't assign to an rvalue.

Share this post


Link to post
Share on other sites

This topic is 3491 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this