Search code examples
javabitwise-or

OR with 3 arguments in java


Why does this statement does not work

boolean b = (y==3-x)||(y==3)||(y=3+x);

but this one does

boolean b = (y==3-x)||(y==3);
        b = b || (y == x-3);

and && statement has no problems with number of arguments passed

boolean b = x < 7 && x >= 0 && y < 7 && y >= 0;

Solution

  • Because in the first case:

    boolean b = (y==3-x)||(y==3)||(y=3+x);
    

    You are doing the assignment not comparison for (y=3+x)

    Change it to:

    boolean b = (y==3-x)||(y==3)||(y==3+x);
    

    and it will work for you

    However in the second case:

    boolean b = (y==3-x)||(y==3);
            b = b || (y == x-3);
    

    You are doing the comparison everywhere thats why it is working for you!

    Also in the third case you are doing comparison

    boolean b = x < 7 && x >= 0 && y < 7 && y >= 0;
    

    NOTE:-

    == is for comparison and = is for assigment.

    <,>,<=,>=, == are all used for comparison