C++ ordering of || and && when testing eqaulity in if statement -
this simple one, can't find answer for.
i'm trying write function returns bool when passed 2 chars of left has higher precedence.
i can achieve required functionality following:
bool precedence(char lhs, char rhs) { if ((lhs == '*' || lhs == '/') && (rhs == '+' || rhs == '-')) return true; return false; } which fine , dandy why how isn't following same:
bool precedence(char lhs, char rhs) { if ((lhs == ('*' || '/')) && (rhs == ('+' || '-'))) return true; return false; } i know wrong, why? me both read indentically. sorry know stupid it's driving me mad.
c++ doesn't define logical operators that.
lhs == ('*' || '/') not mean lhs equals * or /, returns true if lhs 1.
the ('*' || '/') subexpression evaluates true unconditionally, because boolean logic on chars analogous checking if 0 or not. expression looks lhs == true, or lhs == 1.
Comments
Post a Comment