#### [SOLVED] Could anybody explain how "if" statement executed here

By Kumar

According to my limited knowledge logical operators have left-right order of evaluation. By that rule it should evaluate x && y which is 1 and then it should evaluate == 1 in the if statement. But that is what not happening. Could anyone help me with this.

``````int main()
{
int x = 1, y = 2;
if (x && y == 1)
printf("true\n");
else
printf("false\n");
}
``````

#### @Steve Summit 2018-08-10 11:33:09

Try not to think about order of evaluation too much. It will often confuse you. The more important things to focus on is how the operators are grouped together and what the expression means.

The `&&` operator has relatively low precedence. Almost any time you use it, it will have the form

``````if( condition_1 && condition_b )
``````

and the interpretation is the obvious: "if `condition_a` is true and `condition_b` is true".

In your case, `condition_a` is just `x`, and `condition_b` is `y == 1`. So the interpretation is "if `x` is true and `y` equals 1".

What does it mean for `x` to be true? Simply that it's not zero. So we could expand this further: it's as if you had written

``````if ( x != 0 && y == 1 )
``````

and the interpretation is "if `x` is not equal to 0 and `y` is equal to 1".

Remember, precedence says how the operators are grouped together. In your case, it's as if you had written

``````if ( x && ( y == 1 ) )
``````

The `== 1` part goes with the `y`. `==` binds more tightly than `&&`.

In terms of "order of evaluation", you can say that, yes, the compiler emits code that figures out whether `x` is true, and code that figures out whether `y` is equal to 1, before it uses `&&` to determine whether they're both true. In the case of `&&`, we also know that it will decide whether `x` is true before it decides whether `y` is equal to 1. (But this is a rather special property of the `&&` and `||` operators. It does not apply to most of the rest of the more ordinary operators like `+` and `/`.)

#### @Baqer Shah 2018-08-10 11:47:56

try this.

``````int main()
{
int x = 1, y = 2;
if ((x && y) == 1)
printf("true\n");
else
printf("false\n");
}
``````

#### @cse 2018-08-10 11:31:48

This is because of operator precedence.
Because of operator precedence the expression `if (x && y == 1)` results in `if (x && (y == 1))`.
So, the whole expression results to `false` because `y==1` is `false`.

Use parenthesis and change it to `if ((x && y) == 1)` to get desired result.

#### @Achal 2018-08-10 11:32:02

Here

``````if (x && y == 1)
``````

above statement is equivalent to

``````if(x && (y==1) ) /* y==1 performed first, which is false */
``````

As you can see here https://en.cppreference.com/w/cpp/language/operator_precedence

You want to get `true` as output, then first perform `x&&y` by keeping `()`.

``````if ( (x && y) == 1) { /* now () has higher precedence, so it perform x&&y first which results in true(1) and then 1==1 results in true */
}
else {
}
``````

#### @rubenvb 2018-08-10 11:24:43

The order of operations is different than what you think.

``````x && (y==1)
``````

which is `false` in your case.

#### @Kumar 2018-08-10 11:30:17

Oh, I never thought that way, thanks :)

#### @Kumar 2018-08-10 11:47:07

How should I decide where to put () parentheses. Like in this example int z = x << 1 > 5;

#### @rubenvb 2018-08-10 11:49:39

That depends on what you want to do. You can always add unneeded parentheses, but when in doubt, just write two separate statements. Everyone will be much happier (someone else reading your code, or you reading your code in a year's time).

#### @Kumar 2018-08-10 11:51:39

I'm not writing these. These are the questions I'm facing while solving c language quiz.

#### @rubenvb 2018-08-10 11:53:34

Well, then you'll need to look at the table on the page I linked to. The first operator you hit is evaluated first. So replace the operator expression by its result and go through the table again.

#### @Kumar 2018-08-10 12:02:01

Oh, finally got it. It was not as simple as I thought it. You were great help @rubenvb, thanks :)

#### @tilz0R 2018-08-10 11:24:18

Because `y == 1` is false as `y = 2`, written before `if` statement.

It does not evaluate they way you think. It evaluates `x` and then `y == 1` and then `&&`, which is in your case in `1 && 0` and you have `false` result.

#### @Kumar 2018-08-10 11:25:47

So it does follow order of evaluation, right?

### [SOLVED] If left to right and right to left - both associativity of operator are present in a statement then What will be considered?

• 2014-03-19 17:01:23
• Abdus Sattar Bhuiyan
• 617 View
• 5 Score
• Tags:   c associativity

### [SOLVED] order of evaluation of comparison operator?

• 2016-08-04 14:44:15
• rondino
• 230 View
• 2 Score
• Tags:   c

### [SOLVED] Boolean logic evaluation with multiple greater-than operators

• 2014-10-02 11:34:51
• Dave Cooper
• 66 View
• 0 Score
• Tags:   c