If I have the following code:
int a = 1;
bool b = 1;
Does a equal to b? Even the program might return that they are the same, are they actually equal in all aspects in low level?
Also, if I use code (pseudo) such as:
if (a)
then execute();
will execute() run? I am asking for theoretical answers, and I can’t convince myself with experiments as this is not natural science. Thank you all.
I think you can convince yourself with the right experiments:
Perhaps the most important difference between the two is that
boolcan only have two values:trueandfalse, whileintcan have many more. Here’s another experiment that shows a consequence of this:The two types may seem similar because there are implicit conversions between the two. Any integral expression that is zero can be implicitly converted to
false, and any integral expression that is not zero can be implicitly converted totrue. In the opposite direction,falsecan be implicitly converted to zero, andtrueconverted to one. This leads to the code above ending up testing if 2 != 1.Now the answer to the question of whether
execute();is called in the snippet from the question should be obvious: the valueawill be converted to aboolin theifstatement, and since it is not zero, it will convert totrueand result in a call toexecute().