I want both loops to stop as soon as I find an object that has the same position in x.
Here is my C++ code:
for(int i = 0; i < sizeArray; ++i){
for(int j = i; j > 0; --j){
if (s[i].positionX == s[j-1].positionX){
s[i].positionY = s[j-1].positionY;
}
}
}
If I use break;
it only breaks out of the inner for loop. What is the best way to stop both?
Options:
- Set the indexes of the loops to max value (or min value) to terminate the loop.
- Put all this inside a function and use return.
- Use a goto
- Use a Lambda
- Set a boolean stop code to true, break, then listen for break and break out of other loop?
- ?