Doubts with the use of IF / ELSE in C

Asked

Viewed 95 times

2

When I run this code and type sex=M or =F, all conditions are executed as expected. When I type sex=m or =f, only the first condition is met, regardless of the age value.

Can anyone explain me right the use and structure of IF / ELSE ?

int main()
{
    float salB, idade, medica;
    char sexo;

    printf("           PROGRAMA 2");
    printf("\n_________________________________");

    printf("\n\n Informe o sexo do funcionario, digite M para masculino ou F para feminino: ");
    scanf("%c",&sexo);
    fflush(stdin);
    printf("\n Digite o valor do salario bruto: ");
    scanf("%f",&salB);
    printf("\n Digite a idade: ");
    scanf("%f",&idade);

    if (sexo == 'm' || sexo == 'M' && idade < 20)
    {
        medica = (salB*5)/100;
        printf("\n O funcionario pagara R$%.2f de assistencia medica.",medica);
    }
    else if (sexo == 'm' || sexo == 'M' && idade >= 21 && idade < 40)
    {
        medica = (salB*7)/100;
        printf("\n O funcionario pagara R$%.2f de assistencia medica.",medica);
    }
    else if (sexo == 'm' || sexo == 'M' && idade > 40)
    {
        medica = (salB*10)/100;
        printf("\n O funcionario pagara R$%.2f de assistencia medica.",medica);
    }
    else if (sexo == 'f' || sexo == 'F' && idade < 20)
    {
        medica = (salB*2)/100;
        printf("\n A funcionaria pagara R$%.2f de assistencia medica.",medica);
    }
    else if (sexo == 'f' || sexo == 'F' && idade >= 21 && idade < 40)
    {
        medica = (salB*5)/100;
        printf("\n A funcionaria pagara R$%.2f de assistencia medica.",medica);
    }
    else if (sexo == 'f' || sexo == 'F' && idade > 40)
    {
        medica = (salB*7)/100;
        printf("\n A funcionaria pagara R$%.2f de assistencia medica.",medica);
    }
    else
    {
        printf("\n Sexo invalido, tente novamente.");
        system("pause > null");
    }
}

2 answers

1

You have two options, one would be to make a Toupper and compare only with’M' instead of m or M

the second option is as follows::

 if ((sexo == 'm' || sexo == 'M') && idade < 20)

It would look like this: if sex is m OR M, E the age < 20 Parentheses are very important for you to isolate that condition.

  • 1

    This Toupper scheme I didn’t know yet, very good, I will study more about it. Also did not know that have to use more than 1 parenthesis in the conditions to isolate each situation. Thanks Thiago!

1


I have some points that I wanted to help you beyond the answer, well your age control does not control when the age is 20 or 40 and nor when it is negative ( because there is no negative age).

I rewrote the code so that the logic was clearer, managing the sex first and then the age( I didn’t change the logic of age = 20 or = 40):

int main()
{
    float salB, idade, medica;
    char sexo;

    printf("           PROGRAMA 2");
    printf("\n_________________________________");

    printf("\n\n Informe o sexo do funcionario, digite M para masculino ou F para feminino: ");
    scanf("%c",&sexo);
    fflush(stdin);
    printf("\n Digite o valor do salario bruto: ");
    scanf("%f",&salB);
    printf("\n Digite a idade: ");
    scanf("%f",&idade);
    // controle para homens
    if (sexo == 'm' || sexo == 'M')
    { 
        if(idade < 20){
            medica = (salB*5)/100;
            printf("\n O funcionario pagara R$%.2f de assistencia medica.",medica);
        }
        else if(idade >= 21 && idade < 40){
            medica = (salB*7)/100;
            printf("\n O funcionario pagara R$%.2f de assistencia medica.",medica);
        }
        else if(idade > 40){
            medica = (salB*10)/100;
            printf("\n O funcionario pagara R$%.2f de assistencia medica.",medica);
        }
    }
    // controle para mulheres
    else if(sexo == 'f' || sexo == 'F'){
        if(idade < 20){
            medica = (salB*2)/100;
            printf("\n A funcionaria pagara R$%.2f de assistencia medica.",medica);
        }
        else if(idade >= 21 && idade < 40){
            medica = (salB*5)/100;
            printf("\n A funcionaria pagara R$%.2f de assistencia medica.",medica);
        }
        else if(idade > 40){
            medica = (salB*7)/100;
            printf("\n A funcionaria pagara R$%.2f de assistencia medica.",medica);
        }
    }
    else{
        printf("\n Sexo invalido, tente novamente.");
    }
        system("pause > null");
}

There’s also a question of performance, your code makes a lot of control, I mean, first if you’re a woman and you’re under 20, then if you’re a woman and you’re between a period of age, then if you’re a woman and you’re over 40... Good so you do a lot of control and lose a little performance.

What I did was, it makes a check to see the sex, and then it makes the check to see the age, at worst give hypotheses my code makes 4 controls. Yours at worst makes 6. I know it’s a small gain, but gain is always won.

I hope I’ve helped!

  • 1

    Yes, and very much. Thanks for the tips !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.