+ 1

I've just faced a challenge and found this code, I still confused, why does this code output is 1?

https://code.sololearn.com/cEX0mn7EA8Tq/?ref=app

29th Dec 2018, 12:54 PM
mmm...
mmm... - avatar
3 Réponses
+ 5
When it checks if(++a < 0), a is incremented. The condition ++a < 0 is false, that's why a is not increased by 3. You can change it like this to understand it better: int a = 0; if(++a == 1) { cout << "a is 1"; } # output: a is 1. a is increased immediately before its value is checked. That's why the condition ++a == 1 is true.
29th Dec 2018, 1:00 PM
Anna
Anna - avatar
+ 2
In the if condition a has pre-increment, hence it will become 1 and the expression will finally evaluate to false and therefore the body of the condition will not be executed. At the end a will be 1.
29th Dec 2018, 1:25 PM
Umar Sunusi Maitalata
Umar Sunusi Maitalata - avatar
+ 1
Thx Anna
29th Dec 2018, 1:07 PM
mmm...
mmm... - avatar