mardi 7 juillet 2015

C++ ordering of || and && when testing eqaulity in if statement

This is a really simple one, that I can't find an answer for.

I'm trying to write function that returns a bool when passed two chars of which the left has higher precedence.

I can achieve the required functionality with the following:

bool Precedence(char lhs, char rhs) {
  if ((lhs == '*' || lhs == '/') && (rhs == '+' || rhs == '-'))
    return true;
  return false;
}

which is fine and dandy but why how isn't the following exactly the same:

bool Precedence(char lhs, char rhs) {
  if ((lhs == ('*' || '/')) && (rhs == ('+' || '-')))
    return true;
  return false;
}

I know it is wrong, but why? To me they both read indentically. Sorry I know this is stupid but it's driving me mad.

Aucun commentaire:

Enregistrer un commentaire