# super basic question about boolean evaluation

• 02-19-2011
rodrigorules
super basic question about boolean evaluation
Code:

```int var = 1; if(var == 1) { } if(var) { }```
Does the first If statement have to do extra work to convert the "var == 1" into a boolean true false? In other words, does the 2nd if statement skip a step and get treated automatically as a boolean, or is there absolutely no difference in the logic?

• 02-19-2011
Elysia
You should not worry about micro optimizations. Whether or not there is a speed difference will in 99.9999999999999999% of the cases not matter.
The two are not equivalent either.

I wouldn't worry about the details, since you can view them in different ways.
It's true the second is basically a boolean evaluation. However, it is also possible to view it is if (something == true), where the variable will be converted to a boolean expression. So in that sense, it is just the same as the first.
• 02-19-2011
rodrigorules
Yea, I tend to try to get in the habit of writing like that though ... most of the only reason I program is to solve little programming puzzles

THANKS!
• 02-19-2011
whiteflags
They are not the same.

If var was a bool it would be perfectly fine to use it alone, in an if statement. Since var is not bool, it has to have a defined conversion to bool operator in order to compile. The int type has such a conversion defined, so it compiles.

How does an int convert to a bool? Any non-zero value is true.

The conversion takes place in constant time, and then the evaluation for if occurs. By evaluating var == 1 instead, you will avoid converting to bool, but testing equality for integers also occurs in constant time, so there is no real performance difference. But there is a logical difference.

If you mean any non-zero value is true, then the only choice present is the first example, but to the second example, only 1 is true.
• 02-19-2011
Mozza314
I'd recommend not using anything like:

Code:

```int var = 1; if (var) {     // do stuff }```
There's a reason why this kind of thing is illegal in C# and Java - an int is not a boolean and it's hard to read it as though it is one. The only reason C++ supports it is because C++ has to compile C and C has no bool type (well probably also because most early C++ programmers would have been former C programmers and int to bool conversion is a silly idea that C programmers nonetheless have an unnaturally strong affection for).
• 02-20-2011
Elysia
This is not silly. I see no reason why int -> bool conversion should be prohibited. 0 is false, everything else is true. What is so difficult to understand about it? Obviously there will be some who abuse it, but that's the way it is with most programming languages.
And btw, I like the int -> bool conversion even though I have never been a C programmer.
• 02-20-2011
Mozza314
Well... it's not... too.. bad :-P

I don't necessary advocate making int -> bool conversion illegal. But I'm definitely in favour of adopting a style that avoids it. The reason why I say it's silly (maybe 'awkward' would be better way to describe it?) is because the way I see it, implicit conversion is for going between two representations of the same thing, so short -> long makes perfect sense to me, int -> bool is weird.

I guess I'm a platonic idealist at heart. I really like your "The Beautiful C++ Utopia" thingo, but I don't see int -> bool conversion being part of a beautiful utopia.
• 02-20-2011
kmdv
Quote:

Originally Posted by Elysia
This is not silly. I see no reason why int -> bool conversion should be prohibited. 0 is false, everything else is true. What is so difficult to understand about it? Obviously there will be some who abuse it, but that's the way it is with most programming languages.
And btw, I like the int -> bool conversion even though I have never been a C programmer.

I really don't like the implicit int->bool conversion, and so do the java creators. My understanding goes like this:
Whenever I see expression:
Code:

```if (x) { }```
I read it this way: whether x is valid or not? Whether it succeeded or not?

Going this way pointer->bool conversion seems to be reasonable. Matter is different when it is an integer. My intuition would suggest that positive integer is valid and negative is not, and it is not true. I always prefer the explicit form for integers, != or >. I know that I test numbers not booleans.