Hi
I have a question that can I use such a this code:
if (low != mid && mid != high) {
for (int i = 0; i <= mid; i++) {
boolean bool = Determinate.isPointLeftSide(a, auxiliaryListTwo.get(i), auxiliaryListTwo.get(i + 1));
if (bool == false) {
p = auxiliaryListTwo.get(i);
} else {
boolean bool1 = Determinate.isPointRightSide(a, auxiliaryListTwo.get(i + 1), auxiliaryListTwo.get(i));
boolean bool2 = Determinate.isPointRightSide(a, auxiliaryListTwo.get(i + 1), b);
if (bool1 == true && bool2 == true) {
p = auxiliaryList.get(i + 1);
}
else{
i++;
}
}
}
}
I have used “i++” in the else part ,is it correct?
If want the code to have the effect of
i = i +1, then it is correct. Since you already increment i in the for statement, i will be incremented twice if it reaches thatelse. As long as you have analyzed the implications of this in your code, you should be fine. In your case, you won’t have to worry about out of bounds issues because the loop at the top terminates ifi<=midand there’s no code after the i++. However, the loop will never be run for certain values of i when i++ is reached as it effectively skips an iteration.Here’s a quick refactoring of your code to remove useless variables. For maximum clarity, you should put the variables back but use more informative variable names, like
boolean isAToTheLeftOfB = ***. Comments are your friend!!EDIT: Thanks to Vladimir for pointing out that you should use the continue statement instead of incrementing the counter directly. This is definitely more clear and less error prone.