Use of assignment operators inside if condition in

2019-09-30 08:14发布

If I use assignment operators or output functions inside the if condition, why is it taken as a "true" boolean value?

For example, I do

if(cout << "a"){cout << "c";}

Or

if(int x=7){cout << "c";}

Or even

if(6){cout << "c";}

the outputs are indeed, c and c and c in each case. But we were told that, inside the if condition, we have to use an expression that finally evaluates to a boolean value like 1 or 0.

So what is the piece of information I am missing?

标签: c++ syntax
3条回答
闹够了就滚
2楼-- · 2019-09-30 08:35

Every value inside condition (or with boolean conversion) will give you true, unless it equal to 0.

(6)            returns => 6              != 0            => true
(x = 7)        returns => 7              != 0            => true
(cout << "aa") returns => ostream object. 
                          bool context so calls `ostream::operator bool()`
                          If the stream is in good state => true
                          Otherwise                      => false

In c you can see it with NULL with this declaration:

#define NULL 0

which means that if an pointer equal to NULL it will return you a false in a boolean conversion.

Examples in c++ for boolean expressions:

int a, b, *c;
char d;
bool e;

a = 0;
b = 2;
c = nullptr;

e = a; // false
e = b - 2; // false
e = c; // false

c = &a;
e = c; // true
e = b; // true
e = a + 1; // true
e = b ^ b; // false

d = 0;
e = d; // false
d = '0';
e = d; // true
查看更多
The star\"
3楼-- · 2019-09-30 08:36

In all the cases you've shown, it's still true that you've provided an expression that eventually is convertible to bool. If you look at the << operator of std::cout and similar ostreams, you'll see they all return a reference to the same stream. This stream also defines a conversion to bool operator, which is implicitly called in a context where a bool is expected. This particular conversion returns true if the stream has no errors. Secondly, int x=7 is an initialization, and it returns the value that x was just initialized with; 7 in this case. int in C++ can be implicitly cast to bool to be true if it's not zero, which is why both 7 and 6 evaluate to true in the second and third examples.

查看更多
Bombasti
4楼-- · 2019-09-30 08:43

In each case the expression is converted to a bool. The value of this bool is true in the three cases that you use (it may not always be true).

if(cout << "a"){cout << "c";}

In this case the expression:

   cout << "a"

If find the operator<<(std::ostream&, char const*) definition you will find that the return value is std::ostream&. So this will return a reference to the cout object (of type std::ostream). The stream object has a boolean conversion method explicit bool() which can be used to convert the object to bool. The result of this depends on the state of the object (if it is in a bad (failing) state it will return false).

So here you are checking that the last operation on the stream worked. If it does then print "c". This is commonly used on input streams to validate user intput.

int val;
if (std::cin >> val) {
    if (std::cout << "A value was correctly read\n") {
        // Add response worked.
    }
    else
    {
        // Something bad happened to std::cout
    }
}

In the other cases you use int variables. These can be implicitly converted to bool. If the value is non zero then it is true (otherwise false).

查看更多
登录 后发表回答