Today I found a syntax thing I had never seen before. Are ||
and ,
the same?
bool f() {
cout << "f";
return true;
}
bool g() {
cout << "g";
return true;
}
int main() {
if(f(),g())
{
cout<<"OK with ,";
}
cout<<endl;
if(f()||g())
{
cout<<"OK with ||";
}
return 0;
}
From the output of this program, it seems so:
fgOK with ,
fOK with ||
Are they the same exact thing or are there any little differences?
f(),g()
means evaluatef()
then evaluateg()
and returng()
since yourg()
returnstrue
it is that what is returned toif
So, no, the operators are totally different.
You can see the difference if you modify your function to return
false
fromg()
. The conditionf()||g()
will still evaluate totrue
whilef(),g()
will returnfalse
.