[Answered] Multiple OR Operator IF Single VAR is EQUAL To Multiple INT

Is there a better way to do this ?

if ((test == 3) || (test == 5) || (test == 8) || (test == 12)|| (test == 13) || (test == 14)) {
//If test is equal to any of these numbers/Int than do something
}

Thankyou guys

I think you need to encase all those in another set of brackets.

My bad i typed that on the fly. The question still remains. Thanks for noticing that ted.

This might be more readable:

switch(test)
{
    case 3: case 5: case 8: case 12: case 13: case 14:
        /* Code for 3, 5, 8, 12-14 goes here */
    default:
        /* Code for none of the above here (optional) */
        break;
}
2 Likes

If you need to check if a number is in a set of numbers - like you example above I would be tempted to use a list.

 // somewhere in your game
        List<int> levels = new List<int> ( new int[]{3, 5, 8, 12, 13,  14});

        if(levels.Contains(test))
        {

        }

That way, if you ever needed to add more numbers to the set you could just add them. Also you could always serialize that data and allow it to be data driven.

Just an idea.

1 Like

Another example:

    public enum CaseEvent{ Event1 = 0,Event2 = 3,Event3 = 5,Even4 = 8,Event5 = 12,Event6 = 13, Event7 = 14
    }

    public int MyEventAction(CaseEvent thisEvent)
    {
        switch (thisEvent)
        {
        case CaseEvent.Event2:
            return (int)CaseEvent.Event2;
            break;
        default:
            return (int)CaseEvent.Event1;
            break;
        }
    }
  Debug.Log( MyEventAction(CaseEvent.Event2).ToString() );

BINGO I really like that idea Kono especially for versatility so thankyou kindly and thankyou Orb and IronMax for your time.

No time no problem :slight_smile: have fun

@MadeThisName Thinking about this a bit further. As I guess those values will always be unique you should use a HashSet. The Contains test on a HashSet is much faster ( o(1) ) than a list (o(2)) as it optimized for set operations . Also it will bitch if you add two of the same values.

Same code rewritten:

HashSet<int> levels = new HashSet<int>(new int[]{3, 5, 8, 12, 13,  14});

        if(levels.Contains(test))
        {

        }

The only drawback of this is that the Mono implementation in Unity cannot serialize a HashSet (throws a not implemented exception) If its not likely to change you could just have a readonly member variable and initialize in the constructor.

Using a List… why not just use an array? Less garbage, but still some. You can make that array static so that it’s only created once though.

Using a HashSet… yes, much better, get that speed boost of the comparison check. But you now have a HashSet, which is made up of 2 arrays (one for the buckets, one for that actual item slots). Creating yet even more garbage.

Personally, I’d go with the switch/case, keep it clean, keep it simple. Just like Orb and IronMax suggested.

1 Like

@lordofduct

I hope you are not assuming that I am advising him to create that HashSet everytime he does that if check.

I would have thought it obvious you either create it statically or once in a constructor somewhere and constantly reuse it. That was just a code snippet.

I guess your complaints about garbage stem from that assumption? Otherwise what are you getting at? A few extra bytes on the heap? Really?

The reason I recommended using a HashSet is that it is much cleaner, easier to update and allows for the possibility of loading those numbers from another source instead of hardcoding stuff into ifs, enums or switches.

Given the little background information provided about the situation I thought it was a reasonable idea.

Yeah, it’s reasonable, I agreed its comparison check is faster.

You did not show it as a static variable, how do you know the OP would have found that obvious? They could have missed that fact and shot themselves in the foot if this were happening multiple times a frame.

And really, all I did was toss in my vote that a switch/case is what I believe, an opinion, to be the best option. Hence the “Personally…” prefacing my final sentence. And I gave my reasons for why I think the others lack in comparison.

1 Like

Sorry didnt mean for that to come accross as argumentative. Wasnt meant that way.

Youre right about being a bit clearer with code examples. Sometimes I just assume too much.