Home > OS >  How to make conditional statements more compact in C?
How to make conditional statements more compact in C?

Time:10-27

I have a code snippet that uses if-else if-else block. I am wondering any potential ways to shorten the lengthy conditional statement else if (cardLength == 16) && (numberArray[0] == 5 && (numberArray[1] == 1 || numberArray[1] == 2 || numberArray[1] == 3 || numberArray[1] == 4 || numberArray[1] == 5)), for instance, without changing the logic. In python, I can do in this way: if (cardLength == 16) and (numberArray[0:2] in range(51,56)). Are there any specific syntax sugar that I can use fo this purpose in C?

if (cardLength == 15) &&
   (numberArray[0] == 3 && (numberArray[1] == 4 || numberArray[1] == 7))
{
    printf("AMEX\n");
}
else if (cardLength == 16) &&
        (numberArray[0] == 5 && (numberArray[1] == 1 || numberArray[1] == 2 || numberArray[1] == 3 || numberArray[1] == 4 || numberArray[1] == 5))
{
    printf("MASTERCARD\n");
}
else if (cardLength == 13) && (numberArray[0] == 4)
{
    printf("VISA\n");
}
else
{
    printf("INVALID\n");
}

CodePudding user response:

You could convert the first two digits of the numberArray into a new number like this

num = numberArray[0]*10   numberArray[1]

and then use that into the conditional statements to make them more readable

num = numberArray[0]*10   numberArray[1]

if (cardLength == 15) && ((num <= 37) && (num >= 34))
{
    printf("AMEX\n");
}
else if (cardLength == 16) && ((num >= 51) && (num <= 56))
{
    printf("MASTERCARD\n");
}
else if (cardLength == 13) && (numberArray[0] == 4)
{
    printf("VISA\n");
}
else
{
    printf("INVALID\n");
}

CodePudding user response:

Put it into a (static) function. Dont think about performance/optimisation yet.


static char *card_leng_type2string( unsigned len, int *arr)
{
if (len == 15 && arr[0] == 3 && arr[1] == 4 ) return "AMEX";
if (len == 15 && arr[0] == 3 && arr[1] == 7 ) return "AMEX";
 
if (len == 16 && arr[0] == 5 && arr[1] == 1 ) return "MASTERCARD";
if (len == 16 && arr[0] == 5 && arr[1] == 2 ) return "MASTERCARD";
if (len == 16 && arr[0] == 5 && arr[1] == 3 ) return "MASTERCARD";
if (len == 16 && arr[0] == 5 && arr[1] == 4 ) return "MASTERCARD";
if (len == 16 && arr[0] == 5 && arr[1] == 5 ) return "MASTERCARD";

if (len == 13 && arr[0] == 4) return "VISA";

return "INVALID";
}

Now your calling code could just do:

printf("%s\n", card_leng_type2string(cardLength, numberArray) );

CodePudding user response:

Assuming that entries in numberArray are in range 0-9, you could use strchr function. This returns non-NULL if a given string contains a specific character.

Replace:

numberArray[1] == 4 || numberArray[1] == 7 || numberArray[1] == 9

with

strchr("479", '0'   numberArray[1])

If numberArray was an array of character then the check could be simplified to strchr("479", numberArray[1])

CodePudding user response:

If you want the logic intact and only want modification for readbility, you could try adding a preprocessor directive gloabally. This would replace the text or inline function anywhere you use it in the program. Example

#define AMEX_CONDITION (cardLength == 15) &&  (numberArray[0] == 3 && (numberArray[1] == 4 || numberArray[1] == 7))

and use it in your if as

if(AMEX_CONDITION){
    printf("AMEX");
}
  • Related