Best/ most proper way to handle 'if in list' type of logic

Hello all - have a functional remote receiver prototyped, and I was trying to expand it. Right now I have it responding to a single set of IR codes from a single remote. I want to be able to use a handfull of different remotes, each sending different codes. In the end, I want something along the lines of this pseudo code:

var Off_Codes = asdf,fdsa,qwer,rtyu

if (received_code IN OffCodes){ turn off; } etc

I can see how I could do that with nested loops, but it that the right / most efficient/ best way?



Several options, the are books full of this kind of problems

Easiest is to define one String containing all codes,

String CODES = "asdf,fdsa,qwer,rtyu";
int pos = CODES.indexOf(stringToFind);
if (pos != -1)
  // substring found at pos in string Codes

Alternative (better) is use an array and do a lookup with a for loop.

String code[4] = { "asdf","fdsa","qwer","rtyu" };
int pos = -1;
for (int i=0; i<4; i++)
  if (code[i].compareTo(stringToFind) ==0)
    pos = i;
if (pos != -1)
  // string found at pos in array

Thank you! Can I ask what makes one better over the other? The first seems much more succinct...

While the first one is more succinct, if you want to expand it then there is a lot of repeated code. The second one allows you to expand the number of functions simply by adding more elements in the string.

Can I ask what makes one better over the other?

define better?

better can be less code (footprint) better can be faster better can be more maintainable better can be more robust better can be more understandable better can be more adaptability better can be more obfuscated (doubtful but it can) better can be reusable better can be more professional better can be ...... better ;)

As "problem owner" only you can define the measurestick for better ...

The second one is considered good(better) programming practice. Rob