+ 1
Problem with logic operators and pre-incrementation
This code : int x=1,y=1,z=1; cout<<(++x||++y&&++z)<<endl; set x to 2 but y and z stay to 1 (no incrementation) I don't understand why since pre-incrementation takes prority over logical operations. I missed something ?
3 ответов
+ 5
No, you didn't miss anything, and yes pre-increment operator has higher priority over logical OR operator.
++x increments <x> value to 2. Now, remembering that any non zero value is considered truthy, and that logical OR operator only need the first truthy evaluation result to conclude, then <x> value is enough to draw a conclusion.
The rest of the operands to the right of the logical OR operator can be abandoned as the evaluation had been completed.
+ 4
Important rule of or operater if first condition is true it will skip all of the rest conditions
+ 1
Thanks @Ipang and @Geek.
I saw the light !