0

I am new to programming so please don't judge me. This question is simple concept knowledge that I have somehow missed. My question is, do brackets make difference when you are using AND and OR operators? For example,

if ( x == 1 && ( y == 1 || y == 2 ) ){
  // do something
}

Basically, what I want to do is that x has to take value 1 at all times and y can vary between 1 and 2. Does the above code make sense for this purpose? And is it different from:

if ( x == 1 && y == 1 || y == 2 ){
  // do something
}
2
  • 2
    Like in arithmetics, AND behaves like * and OR like +. Commented Jan 14, 2014 at 17:13
  • 2
    it's good habit to always put your conditions in "blocks" using brackets, so instead of if ( x == 1 && ( y == 1 || y == 2 ) ) you type if ( (x == 1) && ( (y == 1) || (y == 2) ) ). Pays off when you start using languages supporting macros or using preprocessor Commented Jan 14, 2014 at 17:23

6 Answers 6

3

Yes, in the first code,‍‍ ‍‍‍‍‍x == 1‍ will be checked first and the result will be checked against the result of y == 1 || y == 2 . and in the second one , x == 1 && y == 1 will be checked at first and the result of it will be checked against || y == 2

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

4 Comments

so you mean in the second one || y == 2 has no use?
no, in second one, the result of x == 1 && y == 1 will be checked, and the result will be check against || y == 2
in the first code I think if x!=1 it wouldn't even go into bracket, so you answer is not correct.
yes, I didn't made my point right, and now I updated my answer
2

Yes, they do. What you're really asking is what is the operator precedence for && and ||?

&& has higher precedence than ||, which means it will be evaluated first if the order isn't specified explicitly using brackets (as you do in your first example).

You can think of precedence as the order in which PHP adds brackets to the expression if you haven't already put them in. In your second example, the operator with the highest precedence is ==, so all instances of these are grouped with brackets:

(x == 1) && (y == 1) || (y == 2)

The next highest is &&, so now we add brackets around these:

((x == 1) && (y == 1)) || (y == 2)

The expression is now unambiguous, so we needn't add any more brackets.

Hopefully it's clear from this argument that the first expression in your post is not the same as the second. For example, if x==0 and y==2, the first expression (correctly) evaluates to false, while the second evaluates to true.

See here for a list of operator precedences in PHP.

1 Comment

+1 for taking time to create a well described answer
1

&& has higher precedence than ||, so it'll be evaluated first. The use of parentheses helps you make sure the order of execution is same as what you expect.

From the PHP manual:

Use of parentheses, even when not strictly necessary, can often increase readability of the code by making grouping explicit rather than relying on the implicit operator precedence and associativity.

Comments

1

The && (logical AND) operator indicates whether both operands are true. The || (logical OR) operator indicates at least one of the operands are true. And...what an operand is?

when you use a simple expression, like x == 1, that is considered an operand.

if you use brackets, everything inside the brackets is consider one operand, no matter how complex it is inside. So you can group conditions.

so x == 1 && ( y == 1 || y == 2 ) will evaluate if the first operand x == 1 is true, and the second operand, the whole ( y == 1 || y == 2 ) is true.

To know if something inside brackets is true or false, whatever it has inside is evaluated as an independent expression.

so, we have to operands. First, it will check if x == 1 is true. If it is false, it wont check what is inside the brackets, it doesnt matter since anyways the first condition is not fulfilled. if x==1, we can check the second operand: so it will check the expresions inside the brackets, this is, y == 1 || y == 2, and if some is true, then ( y == 1 || y == 2 ) is true operand, and the whole expression will be x == 1 && true, so basicaly it now depends on x, and if none inside the brackets is true, then ( y == 1 || y == 2 ) is a false operand, and the whole operation will be x == 1 && false so everything is false

If you dont use brackets, you can achieve the same results, but you will have to check the precedence rules, that will be harder to keep in mind at the begining

I recommend that before you learn a programing language, you read a few tutorials about boolean algebra and logic. Everything will be easier after that.

Comments

0

in first example the left side is evaluated first. if that is true it looks to the right side which is the bracket so it goes to the bracket and evaluates it and than checks against left side.

the second example will not work as you want because if x!=1 it will still go to right side to look for y==1 and you can end up with true.

Comments

0

Brackets don't make a difference. If you want to read more about it, look at the wikipedia page of Boolean logic:

http://en.wikipedia.org/wiki/Boolean_algebra

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.