My boolean expression becomes false in the middle of my loop. What kind of loop should I use?
I keep reading through loop tutorials and each one mentions that a while loop cannot do this. I’ve tried combining a for loop and an if statement and that doesn’t work either. Sorry for the simple question.
}
for(int k = 0;k < collb.length; k++){
//what was used after first calculation
grosssum += col4[k]*mix[k];
one=sum1 - grosssum;
}
for(int n = 0;n < collb.length; n++)
if(one < -need[n] && col4[n] >0){
col5[n]=col4[n]-inc[n] + arrayspecificpounds[n];
net += col5[n]*mix[n];
sum = sum1-net;
} //net is the sum of what was used * mix
//sum1 is what we started with
else{
if(one > need[n] && col4[n] >0){
col5[n]=col4[n]-inc[n] + arrayspecificpounds[n];
net += col5[n]*mix[n];
sum = sum1-net;
}
else col5[n] = col4[n] + arrayspecificpounds[n];
sum = sum1 - net;
}
for(int p = 0;p< collb.length; p++){
if(sum < -need[p] && col5[p] >0){
col6[p]=col5[p]-inc[p] + arrayspecificpounds[p];
net2 += col6[p]*mix[p];
sum2 = sum1 - net2;
}
else{
if(sum > need[p] && col5[p] >0){
col6[p]=col5[p]+inc[p] + arrayspecificpounds[p];
net2 += col6[p]*mix[p];
sum2 = sum1 - net2;
}
else col6[p] = col5[p] + arrayspecificpounds[p];
net2 += col6[p]*mix[p];
sum2 = sum1 - net2;
}
}
for(int q =0;q< collb.length; q++){
if(sum2 < -need[q] && col6[q] >0){
colr[q]=col6[q] - inc[q] +arrayspecificpounds[q];
}
else{
if(sum2 > need[q] && col6[q]>0){
colr[q]=col6[q] +inc[q] + arrayspecificpounds[q];
}
else colr[q] = col6[q] + arrayspecificpounds[q];
}
In this example sum2’s value changes as array col6 is incremented but in the middle of the array the inequalities change. How would I implement a break to stop once sum2 changes?
Here is a simple example of how you can stop a while loop when a boolean value you’re monitoring changes it’s value from false to true:
The same technique can actually be used inside a for loop as well:
This is a bit unorthodox, but it demonstrates how the 2nd parameter in your for loop can be used to monitor a boolean condition that may or may not be related to the actual value of the int i value.