2

I'm interested in knowing if there is a difference between these two if blocks in c ++. It would be very useful if with the answer you can cite some reference.

if ( intVar!= 0 )
{
  //Do something
}

and

if (intVar)
{
  //Do samething
}

Where intVar, could be any type of integer variable with any value.

[EDIT] On the subject "duplicated question". I did not find any question about this in which the if statement is involved.

Rama
  • 3,222
  • 2
  • 11
  • 26
  • 4
    There's absolutely no difference, whatsoever. – Sam Varshavchik Dec 05 '16 at 15:58
  • Possible duplicate of [Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int?](http://stackoverflow.com/questions/4276207/is-c-c-bool-type-always-guaranteed-to-be-0-or-1-when-typecasted-to-int) – Andrew Dec 05 '16 at 16:02
  • 1
    I beg to differ with everybody saying 'there is no difference'. There is clearly a difference. The first version is longer than the second by 5 chars. – SergeyA Dec 05 '16 at 16:18

3 Answers3

7

The type of the expression required in the if condition is boolean. The expression intVar!=0 is already boolean, the expression intVar has type int and requires an implicit conversion to boolean. As it happens the conversion rules for int to bool are precisely that anything non-zero maps to true and zero maps to false, so the resultant expression evaluation is exactly the same.

Sometimes writing the full intVar!=0 can add clarity (for example, to make it clear you're not evaluating a pointer type for nullptr but rather an integral type for zero), whereas other times it doesn't - it really depends on the context.

Regarding requested references, I will use the standard. The section relating to conversions [conv.bool]:

4.14 Boolean conversions

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true

Community
  • 1
  • 1
Smeeheey
  • 9,906
  • 23
  • 39
5

Additionally to other's answers (and for fun), I'd like to say that for an intVar of a user-defined type defining an implicit conversion operator to int and another to bool, the two expression could have a different behaviour:

#include <iostream>

class Celcius
{
    int _value;
public:
    Celcius(int value) : _value(value) {}
    operator int() { return _value; }
    operator bool() { return _value > -273; }
};

int main()
{
    Celcius waterBoilingPoint(0);

    if (waterBoilingPoint != 0) { // false
        std::cout << "This is not Standard Conditions for Temperature and Pressure!\n";
    }

    if (waterBoilingPoint) { // true
        std::cout << "This is not 0K (pun intended).\n";
    }
}

But this is an edge case I wouldn't jump into.

YSC
  • 38,212
  • 9
  • 96
  • 149
4

In C++ (and many other languages) there is no difference as any non zero value is "truey" and zero itself is "falsey".

Jonas Köritz
  • 2,606
  • 21
  • 33