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

28th Jul 2020, 8:28 AM
Bernard Bordja
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.
28th Jul 2020, 8:53 AM
Ipang
+ 4
Important rule of or operater if first condition is true it will skip all of the rest conditions
28th Jul 2020, 8:47 AM
Aayush $aini
Aayush $aini - avatar
+ 1
Thanks @Ipang and @Geek. I saw the light !
28th Jul 2020, 11:11 AM
Bernard Bordja