Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ "OR" operator

can this be done somehow?

if((a || b) == 0) return 1;
return 0;

so its like...if a OR b equals zero, then...but it is not working for me. my real code is:

bool Circle2::contains(Line2 l) {
    if((p1.distanceFrom(l.p1) || p1.distanceFrom(l.p2)) <= r) {
        return 1;
    }
    return 0;
}
like image 638
Jaanus Avatar asked Feb 20 '11 17:02

Jaanus


2 Answers

You need to write the full expression:

(a==0)||(b==0)

And in the second code:

if((p1.distanceFrom(l.p1)<= r) || (p1.distanceFrom(l.p2)<=r) )
    return 1;

If you do ((a || b) == 0) this means "Is the logical or of a and b equal to 0. And that's not what you want here.

And as a side note: the if (BooleanExpression)return true; else return false pattern can be shortened to return BooleanExpression;

like image 97
CodesInChaos Avatar answered Oct 04 '22 22:10

CodesInChaos


You have to specify the condition separately each time:

if (a == 0) || (b == 0))
    bla bla;

When you do

if ((a || b) == 0)
    bla bla;

it has a different meaning: (a || b) means "if either a or b is non-zero (ie. true), then the result of this expression is true". So when you do (a||b) == 0, you are checking if the result of the previously explained expression is equal to zero (or false).

like image 26
Rao Avatar answered Oct 04 '22 21:10

Rao