mercredi 24 avril 2019

Why are all possible integers "true" in the range of long int inside if-statement in C++, but 0 is not?

Basically, how does an if-statement analize whether an integer is true or false? These two simple lines print out "Hello", with a positive integer 764:

int a=764;
if(a){ cout<<"Hello"; }else{ cout<<"Bye"; } //Hello

If i change the integer from positive to negative (-231) it prints out "Hello" as well:

int a=-231;
if(a){ cout<<"Hello"; }else{ cout<<"Bye"; } //Hello

But if i set a to be 0, it gets false

int a=0;
if(a){ cout<<"Hello"; }else{ cout<<"Bye"; } //Bye

It goes true with the range of a long int, from -2147483647 to 2147483647 with only one exception: 0. How does that happen? And what is that if actually doing to determine so?

Aucun commentaire:

Enregistrer un commentaire