Home > Net >  What is the right way to do if structure?
What is the right way to do if structure?

Time:03-19

Firstly, I'm so sorry for my English, I'm still learning. I would like to know what is the correct way to make an if structure?

During the class, my teacher taught that the if-else if structure is more recommended than doing if-if, because as soon as it reaches the true condition, the program leaves the structure. Correct? I'm an intern and it was given to me to do a BMI calculator exercise. I did as follows (following what my teacher explained):

        if (bmi < 18.5)
        {
            Console.WriteLine($"under weigth");
        }
        else if (bmi <= 24.9)
        {
            Console.WriteLine($"ideal weigth");
        }
        else if (bmi <= 29.9)
        {
            Console.WriteLine($"overweith");
        }
        else if (bmi <= 34.9)
        {
            Console.WriteLine($"obesity grade 1");
        }
        else if (bmi <= 39.9)
        {
            Console.WriteLine($"obesity grade 2");
        }
        else
        {
            Console.WriteLine($"obesity grade 3");
        } 

But I was told that this way is incorrect, because my code would be testing all the conditions (which goes against my teacher's explanation). They said the right thing is to do it like this:

    if (bmi < 18.49)
    {
        Console.WriteLine($"under weight ");
    }
    else if (bmi >= 18.5 && bmi <= 24.99)
    {
        Console.WriteLine($"ideal weigth");
    }
    else if (bmi >=25 && bmi  <= 29.99)
    {
        Console.WriteLine($"overweigth");
    }
    else if (bmi >= 30 && bmi <= 34.99)
    {
        Console.WriteLine($"obesity grade 1");
    }
    else if (bmi <= 39.99)
    {
        Console.WriteLine($"obesity grade 2");
    }
    else
    {
        Console.WriteLine($"obesity grade 3");
    }

My question is: why is the first way wrong? I tested both ways and the outputs are the same... Did I misunderstand my teacher's explanation? He explained clearly that he wouldn't need to put the range in if, because if-elseif stops as soon as it gives true value, so there's no risk of continuing validating. I made the code in C# and I will be very grateful if you clarify this doubt, if what I did is right or not... I'm using debug to try to find errors, but nothing... The output of both codes is the same.

Thanks

CodePudding user response:

Your code is right!

If the value is 18.495, then your code works, but not the second code.

The fact that all conditions are checked is not bad, there's nothing wrong with that!

CodePudding user response:

When you construct an if, if else, ... chain it gets only evaluated till the first parenthesis which is true.

Actually your second attempt is flawed, hence you have a gap between 18.49 and 18.50. A value like 18.495 would be interpreted as obesity grade 2. You have to be careful. If should have been like so:

if (bmi < 18.49)
{
    Console.WriteLine($"under weight ");
}
else if (bmi >= 18.49 && bmi <= 24.99)
{
    Console.WriteLine($"ideal weigth");
}
else if (bmi >=25 && bmi  <= 29.99)
{
    Console.WriteLine($"overweigth");
}
else if (bmi >= 30 && bmi <= 34.99)
{
    Console.WriteLine($"obesity grade 1");
}
else if (bmi <= 39.99)
{
    Console.WriteLine($"obesity grade 2");
}
else
{
    Console.WriteLine($"obesity grade 3");
}

CodePudding user response:

Second one is definitely not a right thing cause 18.495 will end up being "obesity grade 2" for example.

As for "right or wrong" - ask your teacher for bmi value which will lead to actually testing different number conditions compared to the suggested solution.

To verify number of if's tested you can add function with side effect returning bool as first condition to every if. For example:

 public static bool SideEffect()
 {
     Console.WriteLine("Side effect");
     return true;
 }

 if (SideEffect() && bmi < 18.5)
 {
      Console.WriteLine($"under weigth");
 }
 else if (SideEffect() && bmi <= 24.9)
 {
      Console.WriteLine($"ideal weight");
 }
 ...

And

if (SideEffect() && bmi < 18.49)
{
    Console.WriteLine($"under weight ");
}
else if (SideEffect() && bmi >= 18.5 && bmi <= 24.99)
{
    Console.WriteLine($"ideal weight");
}
...

I'm pretty much sure that teacher will not able to (except for those "border" ones like 18.495) cause your original tests are in the right order.

CodePudding user response:

Let's say I am a new developer who joined your team and looked at this code.

        if (bmi < 18.5)
        {
            Console.WriteLine($"under weigth");
        }
        else if (bmi <= 50) // This is enough to violate the test cases
        {
            Console.WriteLine($"O V E R   W E I G H T");
        }
        else if (bmi <= 24.9)
        {
            Console.WriteLine($"ideal weigth");
        }
        else if (bmi <= 29.9)
        {
            Console.WriteLine($"overweith");
        }
        else if (bmi <= 34.9)
        {
            Console.WriteLine($"obesity grade 1");
        }
        else if (bmi <= 39.9)
        {
            Console.WriteLine($"obesity grade 2");
        }
        else
        {
            Console.WriteLine($"obesity grade 3");
        }

Look at the condition of Overweight, that a new developer has added on top of the chain. Now if you test, you will see the difference. It's better to control these scenarios over range.

Think about the scenario, where you need to compute something over a range.

  •  Tags:  
  • c#
  • Related