The Gateway to Computer Science Excellence
+2 votes
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
    printf("Hello World");
    int a = 10, b = 20,  c= 30, d = 40;
    int e = ++a || ++b || ++c && ++d;        // DoubtFull Line
    cout<<"a = "<<a<<" b = "<<b<<" d = "<<d<<" c = "<<c<<" e = "<<e; 
    return 0;

Consider the code and consider the precedence table given in the link:-

As per the link mentioned in the above, && operator has higher precedence than $||$, and using the short circuit, first

++c && ++d 

will execute which and return true.

Now the expression should be "++a || ++b || TRUE" in this using the "short circuit principle "only ++a will execute but ++b will not so the final Output should be:-

a = 11 b = 20 c = 31 d = 41.

but the output is 

a = 11 b = 20 c = 30 d = 40

in Programming by Active (1.5k points) | 141 views

1 Answer

0 votes

see precedence of && and | is different thing..  but | is left associative so leftmost of all will be solved first i.e ++a = 11. 

So, TRUE| ++b | ++c&&++d .. short circuit and overall result is true.

O/P- a = 11 b = 20 c = 30 d = 40

by Active (2.3k points)
Why would left associativity of logical OR operator make it execute first than the higher precedence logical AND operator?!

According to me, because of precedence logical AND will be executed first and then among all logical OR operators the leftmost will be executed first. You can even try out by using brackets. In other words, I agree with the reasoning of Jason.
In the question, there is logical OR but explain with bitwise OR.
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,385 answers
105,371 users