-2

If you write this in an if statement (this is an example):

if (A == 1 && B == 1
||
C == 1 && D == 1)

Does Java think like this: if A and B is equal to 1 OR C == 1 and D == 1, then it's true. Or does Java think: if A and B or C is equal to 1 AND D is equal to 1, then it's true. And there's more possibilities with AND and OR..

This is a problem in my programming because I'm not sure about this question.

/Viktor

VisualGhost
  • 49
  • 1
  • 1
  • 7

2 Answers2

2

&& has a higher precedence than ||, so it is interpreted first. i.e., it's equivalent of

if ((A == 1 && B == 1)
    ||
    (C == 1 && D == 1))
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Comparison operator == has an higher priority than the AND logical operator &&, which has an higher precedence than the OR logical operator ||. See documentation for more information: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

In general, if you are not sure about the operators priority, use brackets.

nbro
  • 15,395
  • 32
  • 113
  • 196