OptimusMaximus OptimusMaximus -4 years ago 96
C Question

Is there a shorter way to write nested 'if' conditions?

Just instead of:

if ( ch == 'A' || ch == 'B' || ch == 'C' || .....

For example, to do it like:

if ( ch == 'A', 'B', 'C', ...

is there even a shorter way to summarize conditions?

Answer Source

strchr() can be used to see if the character is in a list.

const char* list = "ABCXZ";
if (strchr(list, ch)) {
  // 'ch' is 'A', 'B', 'C', 'X', or 'Z'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download