Home > Net >  How can I improve these multiple if statements?
How can I improve these multiple if statements?

Time:09-13

    if (gepakteLucifers == 1) {
        printf("De computer pakt 3 lucifers\n");
        printf("%d - %d = %d\n\n", lucifers, 3, lucifers - 3);
        lucifers = lucifers - 3;
    }
    if (gepakteLucifers == 2) {
        printf("De computer pakt 2 lucifers\n");
        printf("%d - %d = %d\n\n", lucifers, 2, lucifers - 2); 
        lucifers = lucifers - 2;
    }
    if (gepakteLucifers == 3) {
        printf("De computer pakt 1 lucifer\n");
        printf("%d - %d = %d\n\n", lucifers, 1, lucifers - 1); 
        lucifers = lucifers - 1;
    }

There are too many if statements. It needs to be simplified but I don't know how.
edit : this is for nim game so when the player picks 3 the computer has to pick 1, if the player picks 2 the computer has to pick 2, if the player picks 3 the computer has to pick 1. And that's it.

CodePudding user response:

You could use a switch statement:

switch(gepakteLucifers){
case 1:
    printf("De computer pakt 3 lucifers\n");
    printf("%d - %d = %d\n\n", lucifers, 3, lucifers - 3);
    lucifers = lucifers - 3;
    break;

case 2:
    printf("De computer pakt 2 lucifers\n");
    printf("%d - %d = %d\n\n", lucifers, 2, lucifers - 2); 
    lucifers = lucifers - 2;
    break;

case 3:
    printf("De computer pakt 1 lucifer\n");
    printf("%d - %d = %d\n\n", lucifers, 1, lucifers - 1); 
    lucifers = lucifers - 1;
    break;
default:
}

However, since the only different operation you're doing inside the if conditions is lucifers = lucifers - something, you could just write a single lucifers -= 4 - gepakteLucifers;:

printf("De computer pakt %d lucifers\n", 4 - gepakteLucifers);
printf("%d - %d = %d\n\n", lucifers, 4 - gepakteLucifers, lucifers - (4 - gepakteLucifers));
lucifers -= 4 - gepakteLucifers;

Moreover, you said the user cannot enter a number lower than 1 or greater than 3, therefore you should check if the input is correct and, if it's not, ask to enter it again. For example using a while():

printf("Input: ");
while ((scanf("%d", &gepakteLucifers) != 1) || (gepakteLucifers < 1 || gepakteLucifers > 3)) {
    printf("Input must be 1, 2 or 3\nInput: ");
}

CodePudding user response:

as per the explanation of yours in the question body,

multiple if conditions can be merged into a single if condition,

what you can do is,

you can make one function which takes the argument as n

like

void func(unsigned int n){
        if ((n > 0 && n < 4) && (gepakteLucifers == n)){
            printf("De computer pakt %d lucifers\n",4-n);
            printf("%d - %d = %d\n\n", lucifers, 4-n, 4-lucifers - n);
            lucifers = lucifers - (4-n);
        }
        return;
}

here you can pass the no. n as 1 2 or 3 or any positive int

it will check the condition and compare the value with the gepakteLucifers and also check for the value range of the entered argument

you also can discard the function and take only the new variable if it is more convenient

hope this may help you to improve the code!!

  • Related