user2979134
user2979134

Reputation:

Java operator "not equal to" help needed

need some explanation of how "!=" works, I took this from past exam paper, in theory ( a==b || a==c || b!=c ) should work but when you compile it says "Scalene" instead of "Isosceles", it doesn't work until I change it to (!( a==b || a==c) || b!=c )).

class test {
public static void main(String[] args) {
    int a = 5;
    int b = 5;
    int c = 10;

    if ( a > 0 & b > 0 & c > 0) { 
        if (a==b && b==c) { 
            System.out.println("Equilateral"); 
        } else if ( a==b || a==c || b!=c ) {
            System.out.println("Scalene"); 
        } else if ( a+b>c && a+c>b || b+c>a ) { 
            System.out.println("Isosceles");  
        }
    }
}

}

Upvotes: 1

Views: 197

Answers (7)

Nils
Nils

Reputation: 806

While evaluating this

if ( a==b || a==c || b!=c )

compiler sees a==b which is true and so doesn't evaluate the expression further.

If you want compiler to evaluate complete expression, use | instead of ||.

Anyway for Scalene triangle you check non-equality of sites not equality. So you may use :

if ( a!=b && a!=c && b!=c )

Upvotes: 0

Aditya
Aditya

Reputation: 1268

The expression (A || B) is evaluated as follows.

If A is true evaluation of the expression stops there and returns true since A || false or A || true is true in any case it wouldn't check for the value of B.

If A is false evaluation is not terminated there as in the above case but then B value is checked. since false || true is true but false || false is false.

As you can see only when the value of A is false B has some significance else it is waste of precious execution time to evaluate B. This is the essence of short circuit evaluation of operators !

The expression ( A && B) is also evaluated in a similar fashion. If A is false the expression evaluation stops there and false is returned. If A is true then the value of B is checked.

For more information on this please read this link.

Upvotes: 0

Voidpaw
Voidpaw

Reputation: 920

The problem in your question has nothing to do with the !=, but with the ||. When it evaluates the statement and it sees that a==b, it says "well, that's correct, so I'm done". If you want it to match all three then you should use && instead of ||.

Upvotes: 0

Yassering
Yassering

Reputation: 226

( a==b || a==c || b!=c ) 

if one of them is true then it will go inside the if clause >

Upvotes: 0

user2975707
user2975707

Reputation:

Operator ! inverts condition. if it is true, it will be false, if false, it will be true.

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

Java expressions are evaluated from left to right. And with comparison operator OR (||) if a condition evaluates to true, rest of the conditions on the right are ignored. Thats why

else if ( a==b || a==c || b!=c )

evaluates to true because a==b results true due to the fact that both a and b are initialized with value 5.

Upvotes: 0

BobTheBuilder
BobTheBuilder

Reputation: 19284

It works as expected.

if a=5, b=5 so (a==b) is true. In that case, ( a==b || a==c || b!=c ) is also true. (because a==b)

Upvotes: 4

Related Questions