In the following code,
int main() {
int a =1, b = 2, c = 3;
if(((a++) == 5) && ((b++) == 5) && ((c++) == 5)) {
cout<<"inside if"<< endl; // prints !!!Hello World!!!
}
cout<<a<<b<<c<<endl;
return 0;
}
all increment operation should be done before doing logical operation. But execution skips increment b and c. Why logical && precede over ()? By the way, result of this code is 223
.
Because of short circuiting: when the left hand side of &&
is false
, the right-hand side is not evaluated. The precedence, on the other hand, is the way you think it should be (and, as AnT says, it's unrelated to the behavior you're seeing): ()
has precedence over &&
.
(Similarly, when the left hand side of ||
is true
, the right-hand side is not evaluated.)