-1

I tried using logical operators in if statements when combining 2 conditions in one IF statement.

I still fail to understand the difference between || and &&, could some one help please?

In this example when I calculate the body mass index value that has been inputted, it doesn't give me the right description, for the 2nd and 3rd IF statement. Also, how do I round BMI result into the nearest 10?

namespace draft {
public partial class Form1: Form {
    public Form1() {
        InitializeComponent();
    }

    private void btn1_Click(object sender, EventArgs e) {
        double weight = Convert.ToDouble(tbweight.Text);
        double height = Convert.ToDouble(tbheight.Text);
        double BMI = weight / Math.Pow(height, 2);
        lbresult.Text = "BMI Result: " + BMI;

        if (BMI < 18.5) {
            lbdescription.Text = "BMI Description: Your weight is extremely low";
            return;
        }
        if (BMI <= 18.5 && BMI < 25.0) {
            lbdescription.Text = "BMI Description: Your weight is in line with average";
        }
        if (BMI <= 25.0 && BMI < 30.0) {
            lbdescription.Text = "BMI Description: Your weight is over";
        }
        if (BMI >= 30.0) {
            lbdescription.Text = "BMI Description: Your weight is excess";
        }

    }
}

}

after running the code

Oso
  • 13
  • 4
  • 3
    This looks like just a typo. You're using `<=` when you meant to use `>=`. For example, `BMI <= 18.5 && BMI < 25.0` should be `BMI >= 18.5 && BMI < 25.0`. (After all, any value that's less than 18.5 *will always be* less than 25.0) The same typo is on the condition after that one as well. – David Feb 27 '20 at 22:00
  • If you use ||, *either* condition can be true and the if-statement will be entered. If you use &&, *both* conditions must be true for the if-statement to be entered. Most conditions can be read out loud as perfectly valid english, using "and" for && and "or" for ||, and it should make sense: "If BMI is less than or equal to 18.5 and BMI is less than 25.0, then..." (as David points out above, it's clear that your "less than" should be "greater than") – Klaycon Feb 27 '20 at 22:03
  • oh ok, my bad, I think this was the problem with it. Both logical operators did not work well. Thank you david! Now I just need to know how to round the result of BMI in window form – Oso Feb 27 '20 at 22:04
  • Indeed, that was my bad. Thank you Klaycon! – Oso Feb 27 '20 at 22:05
  • Yes, but I still have one point left, which is rounding the answer for BMI Result, as you can see in 'after running this code' picture, you can see all the numbers after decimal – Oso Feb 27 '20 at 22:08
  • @Oso I picked a poor dupe target as it's not your exact question, but the method to round is exactly the same.. simply use `Math.Round(num,decimalPlaces)` – Klaycon Feb 27 '20 at 22:10
  • Also, if you use `else if`, you don't have to re-test previous conditions – Rufus L Feb 27 '20 at 22:10
  • Yes my bad, I am thinking of removing this question because it shouldnt have been here to begin with. And thank you Rufus, I will try that – Oso Feb 27 '20 at 22:13

1 Answers1

0

"I still fail to understand the difference between || and &&"

The differences are described in the documentation here, but basically:

  • The result of an && operation will be true if both sides of the operator evaluate to true.
  • The result of an || operation will be true if either side of the operator evaluates to true.

In your case, the use of && is correct, but the problem is the conditions being tested. To test if the value is greater than or equal to 18.5 AND is less than 25, the condition should be:

if (BMI >= 18.5 && BMI < 25.0)

With that said, we can simplify the conditions a little by using else if, which builds upon the original if condition (i.e. the else if is only evaluated if the previous if failed). This means we don't have to test if BMI >= 18.5 if we use an else if, because if we get there it means the previous if condition failed, and that condition was testing if BMI < 18.5.

For example, your logic could be simplified to:

if (BMI < 18.5) 
{
    lbdescription.Text = "BMI Description: Your weight is extremely low";
    return;
}
else if (BMI < 25.0) // if we get here, we know that BMI is >= 18.5
{
    lbdescription.Text = "BMI Description: Your weight is in line with average";
}
else if (BMI < 30.0) // if we get here, we know that BMI is >= 25.0
{
    lbdescription.Text = "BMI Description: Your weight is over";
}
else // if we get here, we know BMI is >= 30.0 
{
    lbdescription.Text = "BMI Description: Your weight is excess";
}

"Also, how do I round BMI result into the nearest 10?"

If you meant the nearest 10th (i.e. first decimal place), you can use the Math.Round() method, and pass it the number of decimal places you want to round a number to. For example:

BMI = Math.Round(BMI, 1); // Will round a number like 18.23 to 18.2 (will round down on .5)

If you mean you want to round to the nearest tens place, then you could divide by 10, round the number, and then multiply by ten:

BMI = Math.Round(BMI / 10) * 10; // Will round to nearest tens (126 will become 130)
Rufus L
  • 36,127
  • 5
  • 30
  • 43