If Else Statement problems chained in C language

Asked

Viewed 128 times

0

I’m starting in programming and had enough difficulty to understand If Else in a chained way. I came across the following situation, when running the code only the first or second condition (If/Else) are possible to be true, regardless of the value

    float Peso, Altura, Imc;
    printf("----------- #17 -----------\n\n");
    printf("Para fazer o calculo do IMC, forneca seu peso: \n");
    scanf("%f", &Peso);
    printf("E sua altura? \n");
    scanf("%f", &Altura);
    printf("Seu peso e sua altura sao: %.2fKg, %.2fm\n\n",Peso, Altura);
    Imc = Peso/(Altura*Altura);
    printf("Seu IMC e: %.2f\n", Imc);
    if (Imc>=40.0)
    {
        printf("------------------------------\n");
        printf("Voce esta com Obesidade Grau III (morbida)\n");
        printf("------------------------------\n");
    }
    else
    {
        if(35.0<Imc<39.9)
                {
                    printf("------------------------------\n");
                    printf("Voce esta com Obesidade Grau II(severa)\n");
                    printf("------------------------------\n");
                }
                else
                {
                    if(30.0<Imc<34.9)
                            {
                                printf("------------------------------\n");
                                printf("Voce esta com Obesidade Grau I\n");
                                printf("------------------------------\n");
                            }
                            else
                            {
                                if(25.0<Imc<29.9)
                                        {
                                            printf("------------------------------\n");
                                            printf("Voce esta com excesso de peso\n");
                                            printf("------------------------------\n");
                                        }
                                        else
                                        {
                                            if(18.6<Imc<24.9)
                                                {
                                                    printf("------------------------------\n");
                                                    printf("Voce esta saudavel\n");
                                                    printf("------------------------------\n");
                                                }
                                                else
                                                {
                                                    if(Imc<18.5)
                                                        {
                                                            printf("------------------------------\n");
                                                            printf("Voce esta abaixo do peso \n");
                                                            printf("------------------------------\n");
                                                        }
                                                }
                                        }
                            }
                }
    }

return 0;

2 answers

1


If you debug the program you will find that if(30.0<Imc<34.9) is always true, that is, that is not the way you will have to do it.

In C you have to make the comparison this way: if(Imc<34.9 && 30.0<Imc)


That is to say, Imc must be less than 34,9 E Imc has to be more than 30.0.

You have to edit all if’s and put it this way.

Not to confuse && with ||(OU) if made if(Imc<34.9 || 30.0<Imc) It means that, Imc must be less than 34.9 OR Imc greater than 30.0

If Imc=5 the condition will be true because ONE of them is true, in this case 5<34.9is true


(@hkotsubo) For complementary purposes only, the operator < returns the value 1 if the comparison is true, and 0 if it is false (see the definition here). So, 25.0 < Imc < 29.9 is interpreted as "the value of the expression 25.0 < Imc is less than 29.9?". Like the expression 25.0 < Imc can return only 1 or zero, it will always be less than 29.9, so the expression will always be true.

  • 1

    For complementary purposes only, the operator < returns the value 1 whether the comparison is true, and 0 if it is false (see definition here). So, 25.0 < Imc < 29.9 is interpreted as "the value of the expression 25.0 < Imc is less than 29.9?". Like the expression 25.0 < Imc can return only 1 or zero, it will always be less than 29.9, so the expression will always be true. I’d forgotten that C had those weird things... :-)

  • 1

    I will edit and put this, thank you

1

Let’s reorganize and simplify this code using the wonderful if else:

float Peso, Altura, Imc;
printf("----------- #17 -----------\n\n");
printf("Para fazer o calculo do IMC, forneca seu peso: \n");
scanf("%f", &Peso);
printf("E sua altura? \n");
scanf("%f", &Altura);
printf("Seu peso e sua altura sao: %.2fKg, %.2fm\n\n",Peso, Altura);
Imc = Peso / (Altura * Altura);
printf("Seu IMC e: %.2f\n", Imc);
printf("------------------------------\n");
if (Imc >= 40) {
    printf("Voce esta com Obesidade Grau III (morbida)\n");
} else if (35 <= Imc && Imc < 40) {
    printf("Voce esta com Obesidade Grau II (severa)\n");
} else if (30 <= Imc && Imc < 35) {
    printf("Voce esta com Obesidade Grau I\n");
} else if (25 <= Imc && Imc < 30) {
    printf("Voce esta com excesso de peso\n");
} else if (18.5 <= Imc && Imc < 25) {
    printf("Voce esta saudavel\n");
} else if (Imc < 18.5) {
    printf("Voce esta abaixo do peso\n");
}
printf("------------------------------\n");
return 0;

Note that the expressions are of the type (25 <= Imc && Imc < 30). That’s so different that (25.0<Imc<29.9). Remember someone may have a BMI such as 29.95, so it is important you watch where you use the <, <=, >and >= to leave no gap in the conditions however small.

You can still simplify a little more:

if (Imc >= 40) {
    printf("Voce esta com Obesidade Grau III (morbida)\n");
} else if (Imc >= 35) {
    printf("Voce esta com Obesidade Grau II (severa)\n");
} else if (Imc >= 30) {
    printf("Voce esta com Obesidade Grau I\n");
} else if (Imc >= 25) {
    printf("Voce esta com excesso de peso\n");
} else if (Imc >= 18.5) {
    printf("Voce esta saudavel\n");
} else {
    printf("Voce esta abaixo do peso\n");
}

For you can take advantage of the fact that the conditions of the ifthe previous ones do not need to be retested in the later ones. For example, if the Imc >= 40 failed and fell into the else, then it’s obvious that Imc < 40 will always be true in this case and so need not even be tested.

Browser other questions tagged

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