+ 1
I've just faced a challenge and found this code, I still confused, why does this code output is 1?
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.
+ 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.
+ 1
Thx Anna