By Kumar


2018-08-10 11:22:38 8 Comments

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

6 comments

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

because of https://en.cppreference.com/w/c/language/operator_precedence

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.

Your expression is equivalent to

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 :)

@rubenvb 2018-08-10 12:12:37

You're welcome. Make sure to accept the answer you feel best answers your question.

@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?

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

1 Answered Questions

3 Answered Questions

[SOLVED] order of evaluation of comparison operator?

  • 2016-08-04 14:44:15
  • rondino
  • 167 View
  • 2 Score
  • 3 Answer
  • Tags:   c

2 Answered Questions

[SOLVED] Experiment With C

  • 2016-05-06 20:00:51
  • Parnab Sanyal
  • 82 View
  • -2 Score
  • 2 Answer
  • Tags:   c

4 Answered Questions

[SOLVED] Require explanation for the output

1 Answered Questions

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

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

1 Answered Questions

[SOLVED] C language prefix postfix issue

3 Answered Questions

4 Answered Questions

[SOLVED] Precedence of && over ||

3 Answered Questions

[SOLVED] Associativity and Precedence in C

Sponsored Content