Reputation: 519
I was wondering if there was a shorter way to write
if (test != 'e' || test != 'd')
I want to write it like
if (test != ('e' || 'd'))
Or something like this so i don't have to repeat "test !="
Thanks
Upvotes: 1
Views: 146
Reputation: 328
C or C++ must evaluate the expressions you write in the syntax of the language. The expression ('e' or 'd') always returns true because the 'or-ing' is done by comparing the bits of the values which will never be the same. How is the compiler to know what you want since in C/C++ a raw character is simply an interpretation of an integer. That's why you can legally write:
char aChar = 'a';
// or
char aChar = 0x41; // hex 41 = ascii 'a'
and have them both work.
Upvotes: 0
Reputation: 13097
That's the syntax of the language. There's not much you can do about it... If you don't like the way it looks, you can make a boolean function that contains the tests and then just call that function:
bool isEOrD(char test)
{
return (test != 'e' || test != 'd')
}
...
if (isEOrD(test))
EDIT: There are other ways to write this code (see the comments to this answer), but your original way is probably the cleanest approach.
Upvotes: 2