Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make conditional statements more compact in C?

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");
}
like image 935
haching Avatar asked Oct 26 '21 10:10

haching


People also ask

What is nested IF condition?

Nested IF functions, meaning one IF function inside of another, allow you to test multiple criteria and increases the number of possible outcomes.

What is conditional branching statement in C?

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.

How do you write a conditional statement in C?

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.

How do you execute more than one statement when a condition is true?

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.

How can I Make my if statement more compact?

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.

Why would you use conditional statements instead of code?

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.

What happens when a condition is true in C++?

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.

What is the difference between IF-ELSE and conditional operator in C?

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.


3 Answers

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) );
like image 159
wildplasser Avatar answered Oct 23 '22 03:10

wildplasser


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;
}
like image 24
Vaggelis_Lazarakis Avatar answered Oct 23 '22 05:10

Vaggelis_Lazarakis


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])

like image 39
tstanisl Avatar answered Oct 23 '22 04:10

tstanisl