Reputation: 83
I'm writing an algorithm and I have a do-while loop in it. I want to be in that loop until either the GAP is small enough or number of iterations is higher than 1000. But it passes 100 iterations and doesn't stop. Here is the code:
int iteration=1;
double UB=0;
double LB=0;
double GAP=1;
do
{...
GAP=abs((UB-LB)/LB);
iteration++;
}while(GAP>=0.05 || iteration<=1000);
Upvotes: 0
Views: 1776
Reputation: 21778
You missed the logic a bit ;)
If you want the loop to end if (conditionA or conditionB), then it means that you want to continue the loop if (conditionA AND conditionB).
In other words, you should have
while(GAP>=0.05 && iteration<=1000)
Upvotes: 2
Reputation: 272497
You probably want &&
instead of ||
. You want to iterate until either of those expressions is false, therefore you want to iterate while both of them are true.
Upvotes: 4