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");
}
Nested IF functions, meaning one IF function inside of another, allow you to test multiple criteria and increases the number of possible outcomes.
A programming instruction that directs the computer to another part of the program based on the results of a compare. High-level language statements, such as IF THEN ELSE and CASE, are used to express the compare and conditional branch.
C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.
To have more than one statement execute after an if statement that evaluates to true, use braces, like we did with the body of the main function. Anything inside braces is called a compound statement, or a block.
This makes the code more compact, which then makes it easier to see what the if statement actually does. Let’s start and see how we can make our if statements better. The first way to simplify if code is to turn a complex if statement into a regular if statement and nested if statement.
Sometimes it's easier to express a conditional that's the opposite of what you want to check in code. For example, if you expect input between 1 and 10 (inclusive), and want to show an error when you don't get something in that range.
If the condition ( a < b) is true, execution will move to the statement following the while condition, in this case the empty statement specified by the semicolon. After executing that statement (doing nothing), execution returns to the condition.
The if-else statement takes more than one line of the statements, but the conditional operator finishes the same task in a single statement. The conditional operator in C is also called the ternary operator because it operates on three operands. The conditional operator is also known as a ternary operator.
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) );
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
int num = numberArray[0]*10 + numberArray[1]
if ((cardLength == 15) && ((num == 37) || (num == 34)))
{
printf("AMEX\n");
}
else if ((cardLength == 16) && ((num >= 51) && (num <= 55)))
{
printf("MASTERCARD\n");
}
else if ((cardLength == 13) && ((num >= 40) && (num <= 49)))
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
return 0;
}
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With