views:

376

answers:

6

I have a set of three values, call them x, y, and z. If value A happens to match only one in the set x, y, and z, then that means we have a proper match and we stop searching for a match, even if it is at y. It can match any one in that set. These values x, y, and z are non-constant so I cannot use a switch-case statement.

How do I do this with an if-elseif-else statements without having to use GOTO. I am using C++ (no boost or any of that other fancy stuff).

Now, I am trying to do this in code and it is racking my brain this morning (not enough coffee?)

+21  A: 

Um:

if ( a == x || a == y || a == z ) {
   // whatever
}

Or am I missing something?

anon
Next time please read the question more carefully. Sheesh. He clearly said, "without having to use GOTO". You'd think somebody with 80k experience points would know that.
Swingline Rage
@Swingline Sarcasm? Irony?
anon
Does nobody else think OP's original question was a joke? Look at some of his other questions. http://stackoverflow.com/questions/2017510/a-better-way-to-build-a-packet-byte-by-byte. Guy can do hex packet building and knows not to use GOTO but doesn't know how to do an IF statement? Has earned 3K points but doesn't know how to test for a, b, and c in an IF?? Really???
Swingline Rage
@Swingline: The questions are not jokes. The packet building question was to see if there was a better way other than just specifiying the hexadecimal bytes in a constant fashion, as you can see in the answers, there is more than 1 way to skin a cat. No such thing as stupid questions, just stupid answers - like yours. That question garnered over 200 views. Take a look at my other questions and answers.
0A0D
Remind me never to hire you. You obviously can't tolerate a little good-natured ribbing. And since you felt the need to call my "answer" stupid (hint: I posted it as a comment specifically because I didn't feel it was worthy as an answer), I have to say: your question is one of the most pointless I've seen on SO. That's why the lead answer (with 20 upvotes) begins with a single word: "Um". Sorry to be unpleasant but you should really reign in the geek rage. Take it from a guy who knows.
Swingline Rage
@Swingline Remind me never to hire someone who can't work out how the very simple SO comment system works. WTF are you commenting on/replying to???
anon
@Neil: I'm responding to Changeling's comments, not to your answer.
Swingline Rage
+1  A: 

In PHP, I'd do if(in_array('A', array($x, $y, $z)) { }. I imagine you can do something similar in C++.

ceejayoz
Why is this negative ? I like this approach.
tomdemuyt
@tomdemuyt: I didn't mark it negative, but probably because I specified C++
0A0D
@tomdemuyt - I didn't downvote, but my guess would because the question is tagged C++ and in C++ the question is actually quite trivial while this is much more complex.
Justin Niessner
@JUstin Niessner This is more complex with just three things to check, but what if your list of conditions is 100 different possibilities? An array would be a lot nicer in that sort of situation than 100 conditionals ORed together.
ceejayoz
@ceejayoz - True. For a large set, a Contains style method would be easier.
Justin Niessner
+14  A: 

The || operator in C++ short-circuits and will stop evaluating as soon as it hits true, so:

if (a==x || a==y || a==z)
{
}

Will do exactly what you want.

Justin Niessner
+1 for the explanation.
Alan
+1  A: 

Most efficient way I can think of, assuming I understood your question rightly:

int mx = a == x, my = a == y, mz = a == z;
if (mx + my + mz == 1) {
    if (mx) {
        // x
    } else if (my) {
        // y
    } else {
        // z
    }
} else {
    // no match
}
anon
+2  A: 

STL has std::set. std::set has find().

Dummy00001
+1  A: 

By non-constant do you mean you may have [X], or you may have [X,Y] or you may have [X...]

If so put the values into a vector and use binary_search (both vector and binary_search are in STL)

Vector<int> itemVec;
itemVec.push_back(x);
//push values as needed

if( binary_search( itemVec.begin(), itemVec.end(), a)
{
   //do something
}
David Relihan