my question is related to appearance of the code and how is it doing in terms of performance.
let's say i have an enum:
typedef enum {
THIS_IS_A_VALUE1 = 65,
THIS_IS_A_VALUE2 = 10,
THIS_IS_A_VALUE3 = 45,
THIS_IS_A_VALUE4 = 5,
THIS_IS_A_VALUE5 = 7
} I_AM_AN_ENUM;
and i want to check whether a certain variable contains one of the values in the enum: i thought about three ways to do it:
1.
int val;
if(val != THIS_IS_A_VALUE1 && val != THIS_IS_A_VALUE2 && val != THIS_IS_A_VALUE3...)
{
// val is different than all values
// do something
}
2.
if(val != THIS_IS_A_VALUE1)
{
// keep blank
}
else if(val != THIS_IS_A_VALUE2)
{
// keep blank
}
else if(val != THIS_IS_A_VALUE3)
{
// keep blank
}....
else
{
// val is different than all values
// do something
}
3.
switch(val)
{
case THIS_IS_A_VALUE1:
case THIS_IS_A_VALUE2:
case THIS_IS_A_VALUE3:
..
..
default:
// val is different than all values
// do something
}
to be honest, i find 2 and 3 ugly, especially when there are a lot of values in the enum. the first way can also be pretty ugly but better then the others, though using a lot of && can reduce from the performance from my understanding.
so to summarize: readability and performance are very important to me, and i'm really unsure which of the options is the best, or maybe there are other ways to do it?
thanks.
EDIT: i forgot to mention that the values are not sequential, that's the case and I can't really change it.