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).