开发者

using "i++" in a for loop

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, auxilia开发者_高级运维ryListTwo.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?


the basic rule is that you don't change the counter variable(i here) in any way inside the loop. If you want to skip the current iteration you should use continue; statement.


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 that else. 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 if i<=mid and 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!!

package example;
        if (low != mid && mid != high) {
        for (int i = 0; i <= mid; i++) {
            if ( ! Determinate.isPointLeftSide(a, auxiliaryListTwo.get(i), auxiliaryListTwo.get(i + 1))) {
                p = auxiliaryListTwo.get(i);

            } else {
                if ( Determinate.isPointRightSide(a, auxiliaryListTwo.get(i + 1), auxiliaryListTwo.get(i)) && Determinate.isPointRightSide(a, auxiliaryListTwo.get(i + 1), b) ) {
                    p = auxiliaryList.get(i + 1);
                }
                else{
                    i++;
                }
            }
        }
    }

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.


Can I offer a couple of style tips?

Try not to use double negatives. E.g.

if not condition
  task for false
else 
  task for true

This can be difficult to read. It's clearer to say

if condition
  task for true
else 
  task for false

It's also not normal to say:

if (bool == false)
  do something

We normally say

if !bool
  do something

And we would not use a variable called bool but would name according to purpose. E.g.

boolean isLeftSide = Determinant....


Not commenting on your code particularly but it does not feel right to do that. A for loop is self managed in the sense that i is incremented for you. When you also manipulate the value of i the results are difficult to predict. Potentially you can find yourself going off the edge of a collection when you do such.


You might want to use while loop, for things to look more logical. This is perfectly legal, but not a 'good' way to code.


`for (int i = 0; i <= mid; i++)

already increments if its true or false so if u use again in else it increments twice

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜