if (condition) vs if (condition=true)

What is the difference between those two examples?

boolean x=false;

1------
if (x){
do some stuff
}

2------
if (x=true){
do some stuff}
}

My program works with the first example but not the second.

Last edited on
(2) is doing an assignment. The return value of an assignment is a reference to the current value of the object just assigned. In other words, in (2) you are assigning true to x, and then evaluating if (true), which is always, well, true.

== tests for equality.
= is an assignment. Avoid doing assignments in if-statements, generally.

if (x == true)
and
if (x)
are equivalent (for bool values).
Last edited on
Well, thanks Ganado, I forgot to write one more equal sign in the program.
I meant to write , if (x==true) not if (x=true).
If you had written if (true = x)
then the compiler would have reported the assignment as error.
Some do not like the "reverse" style.

A good compiler should warn about the "valid" assignment in condition.


If the x is not bool, then if (x) starts by converting the value of x to bool before the boolean expression is evaluated. An implicit version of these:
1
2
if ( static_cast<bool>(x) )
if ( bool(x) )


For example, the std::istream has custom conversion-to-bool member function.

If the x is not bool, then if (x==true) starts by converting the value of x to bool before the boolean expression is evaluated.

The if (x=true) starts by assigning the boolean value to x and then the new value of x is converted to bool (if it isn't a bool).
If the x is not bool, then if (x==true) starts by converting the value of x to bool before the boolean expression is evaluated.

The conversion rules is a bit more complicated. The important thing to realize is that both operands will be converted to the same type before the comparison takes place. If x is an integer, then true will be converted to an integer with value 1.
> that both operands will be converted to the same type before the comparison takes place.

If the type of x is a class type, things could be more interesting. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

struct A
{
    int i ;

    A( bool b = false ) : i(b) { std::cout << "A::constructor\n" ; }

    bool operator== ( bool v ) const
    {
        std::cout << "bool A::operator== (bool) const\n" ;
        return v ? i : !i ;
    }

    operator int () const
    {
        std::cout << "A::operator int() const\n" ;
        return i ;
    }
};

struct B
{
    A operator== ( A a ) const
    {
        std::cout << "A B::operator== (A) const\n" ;
        return a ;
    }
};

int main()
{
    {
        const A x ;

        std::cout << "\n-------------------\n\n" ;
        if( x == true ) ; // bool A::operator== (bool) const
    }

    {
        const B x ;
        std::cout << "\n-------------------\n\n" ;

        if( x == true ) ; // step 1. convert bool (true) to A - A::constructor
                          // step 2. call A B::operator== (A) const, get result of type A
                          // step 3. convert the result A to int - A::operator int() const
                          // step 4. contextually convert int to bool
    }
}

http://coliru.stacked-crooked.com/a/6e6ceb3359f76ca8
Topic archived. No new replies allowed.