1

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"); } } } 

}

4
  • One word: short-circuit. Commented Nov 11, 2013 at 12:47
  • when compiler sees a==b || is true, the rest is not important Commented Nov 11, 2013 at 12:48
  • "in theory ( a==b || a==c || b!=c ) should work" No it shouldn't, you're doing the opposite test to what a scalene triangle is. Not sure if the issue is that you don't know how basic operators and conditions in Java work or you don't know what a scalene triangle is. Commented Nov 11, 2013 at 12:52
  • Also (!( a==b || a==c) || b!=c )) is incorrect for testing if the triangle is scalene; if you switch the values of a and c that still tells you that the triangle is scalene when it isn't. Commented Nov 11, 2013 at 13:03

7 Answers 7

4

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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 ||.

Comments

0

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.

Comments

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

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

Comments

0

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.

Comments

0

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 ) 

1 Comment

That's still wrong, it should be && not ||. What if a != b is true, but a != c is false (i.e. sides a and c are of equal length)? That's still going to return true, even though the triangle isn't scalene.