1. ## Boolean Logic Question

Ok... what am I missing? I just don't get why the following code doesn't work. What my code is trying to do is say, 'if the variable is equivalent to a list of possible results, print match. Otherwise, print no match.'

Code:
```#include <iostream.h>
#include <stdlib.h>

int main()
{

int x = 5;

if( x == (5 || 3 || 2 || 1 || 0) )
cout << "match" << endl;
else
cout << "no match" << endl;

system("PAUSE");

return 0;

}```

2. PS I know I can fix this with something like:

if( (x == 5 || (x == 4) || ... etc) )

but that's kind of ugly. What are the alternatives?

3. Code:
```#include <iostream.h>
#include <stdlib.h>

int main()
{

int x = 5;

switch (x)   // http://www.cprogramming.com/tutorial/lesson5.html
{
case 5:
cout << "match" << endl;
break;
default:
cout << "no match" << endl;
break;
}

system("PAUSE");

return 0;

}```

4. Code:
```#include <iostream.h>
#include <stdlib.h>

int main()
{

int x = 5;
int match = 0;
for (int i = 0; i < 6; i++)
{
if ( x == i )
{
cout << "match" << endl;
match = 1;
}
}
if (match == 0)
{
cout << "no match" << endl;
}
system("PAUSE");

return 0;

}```

5. ## Re: Boolean Logic Question

Originally posted by Unregistered
Ok... what am I missing? I just don't get why the following code doesn't work. What my code is trying to do is say, 'if the variable is equivalent to a list of possible results, print match. Otherwise, print no match.'

Code:
```#include <iostream.h>
#include <stdlib.h>

int main()
{

int x = 5;

if( x == (5 || 3 || 2 || 1 || 0) ) // if (x <= 5  && x != 4) will work
cout << "match" << endl;
else
cout << "no match" << endl;

system("PAUSE");

return 0;

}```

6. I'm pretty sure that the original poster needs assistance for the problem in general of detecting equality of a variable and at least one member in a set. If that is true then the above methods probably won't help a whole lot, but then again, as far as I know, nothing will.

7. Let's say we have a global array of 10 elements. If the array is sorted, then you can use binary search (I belive that's what it's called) which checks halfway through the array. If the value there is more than the number, it simply ignores the top half of the array and performs the action on the lower half until the number is found. Vicaversa, if the number is above the midway point, it ignores the lower half and recursively checks the upper half until the number is found.