Simply way to do multiple OR in IF?

Is there a simpler way to do this:

if( x=="a" || x=="y" || x=="c")

?

At somepoint I used a bit of code like

if(x==("a"|"y"|"c"))

but i seem to remember it only worked on certain types?

any ideas?

cheers

monkey

Is that so complex? It’s easily readable by humans and there is no question to what it means. You may be able to shorten it in the code but the end result after compilation is what matters.

is there a quicker/neater/other way to do this?! :rolleyes:

That’s just not necessary.

There are different ways you could approach this such as switch/case/break or returning a bool but the questions are why and does it make a difference in the compiled code? My guess would be probably not. Shortening code does not guarantee or even imply improved performance and can cause more harm than good.

Apologise for the eyerole, but your original, un-edited response was a little unhelpful!

I get your point but let me rephrase:

At somepoint I used a bit of code like

if(x==("a"|"y"|"c"))

but i seem to remember it only worked on certain types - it is the single bar explanation i need

cheers

Well or operands only work with type bool. Those are just strings, not conditions. If x is a string (which I assume due to the comparison to quoted text) maybe you’re thinking of regular expressions.

OK, I think I remember where I used it now, seemed a neat little trick so i thought i’d give it a go on a string! - thanks for your help :slight_smile:

monkey

As a side question, do you know the difference between the single pipe | and the double pipe || operators?

Not really - all i know is that double will only keep evaluating until it reaches a false.

That’s sort of it. || will stop evaluating when it hits a true and set the result to true. Same with && and &. && will stop evaluating when it hits a false and set the result to false.

That’s what i meant! So what does the single do and when would i need it?

Honestly, I have no idea why you’d want to use the | or & I’ve never had a reason to use either.

I think you are talking about bytemasks used on enum types – they support that syntax.

I’d generally argue there are lots of better ways to do this than multi-clause if statements.