Problems with If Else Statement chained in C language

0

I'm starting programming and I had a hard time understanding 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;
    
asked by anonymous 24.08.2018 / 00:10

2 answers

1

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

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

That is, Imc must be less than 34,9 E Imc must be greater than 30.0.

You have to edit all if's and put it that way.

Do not confuse && with || ( OU ) if you 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 is true because just ONE of them is true, in this case 5<34.9 is true

(@ hkotsubo) Just to complement, the < operator returns the value 1 if the comparison is true, and 0 if it is false (see definition here ). Therefore, 25.0 < Imc < 29.9 is interpreted as "the value of the expression 25.0 < Imc is less than 29.9 ?". Since 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.

    
24.08.2018 / 00:24
1

Let's rearrange 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 type (25 <= Imc && Imc < 30) . This is very different from (25.0<Imc<29.9) . Remember someone may have a BMI such as 29.95, so it's important to pay attention to where you use < , <= , > and >= to leave no gap in any conditions whatsoever. p>

It's still a bit easier to simplify:

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");
}

You can take advantage of the fact that the conditions of the previous% s of% s do not need to be retested in the later ones. For example, if the if test failed and fell into Imc >= 40 , then it is obvious that else will always be true in this case and therefore does not even need to be tested.

    
24.08.2018 / 00:27