0

I have code like this in C:

if (count == 5 || count == 8 || count == 9 || count == 10)
{
    // Do something
}

Is there a possibility to write this shorter? Something like:

if (count == 5, 8, 9, 10)
{
    // Do something
}

5 Answers 5

4

In some cases, it can make sense to use a bit map.

For example, your test:

if (count == 5 || count == 8 || count == 9 || count == 10)

is equivalent to:

if ((1<<count) & 0x720)

The values 5, 8, 9, and 10 are encoded in the bits of the value 0x720.

Typically this would make sense if you have meaningful symbolic constants for the values 5, 8, 9, and 10, with 0x720 constructed from a bitwise "or" (|) of those constants -- which would result in more verbose code than the simple if you have in your question.

In practice, real-world code should minimize the use of "magic numbers". It's difficult to tell what the values 5, 8, 9, and 10 mean.

Possibly you should consider using a different algorithm, but it's impossible to tell without more information.

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

2 Comments

Yes, maybe there is a better solution to my problem, but it doesn't matter since this is just a small piece from my programming homework, and i dont want to post the whole task.
To avoid undefined behaviour, only do this if it is known that count < 31 ! (or whatever your int size is)
4

Depending on how many different values and different branches you have it can sometimes be neater/easier to use a switch:

switch (count)
{
case 5:
case 8:
case 9:
case 10:
    do_something();
    break;
default:
    do_something_else();
    break;
}

1 Comment

I have just my 4 values. Just wanted to shorten the code. count is max. 16.
3

There is no way to insert a comma-separated list into an if statement itself as in your example, but you may write something like this to use the comma-separated list format.

int allowed_counts[] = {5, 8, 9, 10};
int i;
for (i = 0; i < 4; i++) {
    if (count == allowed_counts[i]) {
        ...
        break;
    }
}

Although, a switch statement is more computationally efficient for all list sizes.

1 Comment

@Gugi: Depending on what you're doing, the straightforward if statement in your question might be the best way to do it. It's more readable than most of the alternatives, and for something this small performance isn't likely to be a significant issue.
0

I think you're looking for something like this:

So the condition is testing if count is 5, 8, 9, 10:

if (count == 5 || (count >= 8 && count <= 10 ))
    printf("count is 5, 8, 9 or 10");

6 Comments

Unless count equals 6 or 7!
@Rizier123: The original code wanted only 5, 8, 9 and 10 to succeed at the conditional. Your code also succeeds at 6 and 7.
@Rizier123: No, it doesn't. Try it yourself, with count == 6. The code in the question tests for count equal to 5, 8, 9, or 10, not for a range.
yes, this would work, but it's just a bit better, i wanted something very short and for every scenario. so it should also work when count is 4,6, 8, or 11.
@SimonGuggi i don't think you can get that spesific condition any smaller! (What you mean it also should work if count is 4,6,8 or 11? You mean in the condition that the same should happen like if count is 5?)
|
0

E.g EXIST(count, (5,8,9,10) expand by macro of boost

#include <boost/preprocessor/tuple/to_seq.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/control/if.hpp>

#include <stdio.h>

#define F(r, data, i, elem) BOOST_PP_IF(i, ||, ) (data == elem)
#define EXIST(var, values) BOOST_PP_SEQ_FOR_EACH_I(F, var , BOOST_PP_TUPLE_TO_SEQ(values) )

int main(){
    int count;
    scanf("%d", &count);
    if(EXIST(count, (5,8,9,10)))//if( (count == 5) || (count == 8) || (count == 9) || (count == 10) )
        printf("yes\n");
    else
        printf("no\n");
    return 0;
}

Comments

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.