Problems with If Else statement chained in C language
I'm starting in programming and had a lot of difficulty 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;
2 answers
If you debug the program you will check that if(30.0<Imc<34.9)
is always true, that is, it is not that way you will have to do it.
In C you have to make the comparison this way:
if(Imc<34.9 && 30.0<Imc)
I.e. Imc
must be less than 34,9 E
Imc
it has to be greater than 30.0.
You have to edit all if's and put that way.
Do not confuse &&
with ||
(OU
) if you do if(Imc<34.9 || 30.0<Imc)
Means that Imc
must be less than 34.9 or Imc
greater than 30.0
If Imc=5
the condition is true because only one of them is true, in this case 5<34.9
is true
(@hkotsubo) just to supplement, 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 " is the value of the expression 25.0 < Imc
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.
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 expressions are of type (25 <= Imc && Imc < 30)
. This is quite different from (25.0<Imc<29.9)
. Remember someone may have a BMI such as 29.95, so it is important you pay attention 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 previous if
s do not need to be retested in the subsequent ones. For example, if the test of Imc >= 40
failed and fell on else
, then it is obvious that Imc < 40
will always be true in this case and so it does not even need to be tested.