I saw some c++ syntax that puzzled me
int x = 10;
if(x){ //this line confused me
//code
}
else{
//code
}
i don't understand how this is valid code, what does the if(x)
do?
An int
converts implicitly to a bool
. Any int
that's non-zero evaluates to true
. A zero integer converts to false
. In your case, that line basically tests whether x
is different from zero, and it is equivalent with
if(x != 0) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With