-1
if (i != 0 && i != 15 && i != 20)
{
    ...
}

For the above code snippet, how can I combine all the conditions so I won't have to use multiple comparison operators?

8
  • 2
    Sometimes a bit of context is important. How many conditions are you expecting to put there, and what problem is being solved? There might be a more practical way to express it. Commented Aug 11, 2023 at 5:58
  • 5
    Put all the values to check against in an array. Use a loop to check each value one by one. Set a flag to indicate state, and break early if i == value. Commented Aug 11, 2023 at 5:58
  • 2
    For straight-up equality testing with integers, you could jam them all into a switch statement, and for != put the code block into the default section. Whether that's "nice", or "readable" depends very much on the task at hand. It's more stuff to type, so that's probably not what you're asking. Commented Aug 11, 2023 at 5:59
  • 4
    C doesn't really have a shortcut for this. Commented Aug 11, 2023 at 6:10
  • 1
    if (!inarray(i, (int[]){0, 15, 20}, 3)) { /* ... */ } ... the inarray() function implementation is left as an exercise Commented Aug 11, 2023 at 7:19

2 Answers 2

1

In other languages, you might use something like:

if (!(i in (0, 15,20)))
{
    ...
}

But as far as I know, this does not work in C.

So there's no way out of this.

However, I would advise you to use the following formatting:

if (i != 0  && // meaning of  0 value
    i != 15 && // meaning of 15 value
    i != 20)   // meaning of 20 value
{
    ...
}

As you see, putting every single condition on a separate line increases readability and gives you the opportunity to add comments for that one condition. The usage of such comments is seen as a burden by starting programmers, but once you have experienced the burden of needing to re-think a piece of code, multiple months after having developed it, you'll understand the usage of such comments :-)

Sign up to request clarification or add additional context in comments.

1 Comment

"But as far as I know, this does not work in C." Or rather in C you'll have to implement the function yourself. After which your code probably runs much faster than "fancy language x"... and maybe contains a few more bugs as well :)
0

If you only have 3 checks to perform, the chained && in your original code is probably the most readable and shouldn't be changed. Just keep De Morgan's Laws in mind when negating boolean expressions - getting those wrong is a common bug.

You could use a loop, but that's probably only sensible if you have a whole lot of checks to carry out than just 3:

const int conditions[] = {0, 15, 20};
bool ok= true;

for(size_t i=0; i<sizeof conditions/sizeof *conditions; i++)
{
  if(n==conditions[i])
  {
    ok = false;
    break;
  }
}

if(ok)
{
  ...
}

For values less than 256 you could also perhaps use memchr, though that's also more obscure than what you already have:

#include <string.h>

const unsigned char conditions[] = {0, 15, 20};
if(memchr(conditions, n, sizeof conditions) == NULL)
{
  // n does not equal any of the conditions
}

1 Comment

And for a lot of conditions, bsearch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.