1

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

1
  • 7
    There's a much shorter way of writing that: if (true). You might want to take a look at DeMorgan's Laws though. In case you're wondering, if test is d, it won't be e, and if it's e, it won't be d. Therefore your condition will always be true. You probably want &&, not ||. Commented Apr 30, 2012 at 1:30

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Yeah that just isn't worth the trouble for such a minor issue that only has to do with simplifying. I'm only checking for 'e' or 'd' once so this would be over complication but I can see how it would be useful if I used it more than once.
This is C++, of course theres ways to do whatever crazy you want. Boost has bits vaguely like this for adding several elements to a container.
@JDN Answering is hard on my phone, but it can be done with std::accumulate, std::bind1st, std::bind, and a function that compares inequality (theres probably a std:: for that, but I dont know it off the top of my head.
I hardly think this is a more readable solution. This is over complicating of a trivial boolean check. You shouldn't need STL help to do this.
@Oleksi: I agree that STL is overkill, think the code in the OP was best. But the STL answer is interesting, flexible, and uses fewer characters as number of inequalities grows very large. I only suggest it because this answer isn't flexible. I like the readability though.
|
1

You could use the old C function strchr:

if (!strchr("de", test)) { // test is not 'd' or 'e' } 

But I don't know whether it is any nicer to look at… personally I would probably just have the two !=.

2 Comments

@MooingDuck: I agree... but I've seen it in a few places, although moreso in C projects rather than C++ projects.
The strchr thing is not bad if you have 43 different alternatives, but for 2, it's needlessly indirect.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.