-4

I've searched this question here before asking and all of the answers seem to be people putting code after return break or others. I am having an issue where no matter what I put in an if statement, the code reads that it is unreachable.

private const double quarterPrice = 4.50;
private const double halfPrice = 7.50;
private const double fullPrice = 10.00;
private const double taxRate = .08;
private int orders = 0;
private double sales = 0;

private void btnFindMax_Click(object sender, EventArgs e)
{
    if (quarterPrice > halfPrice)//if i put something in here, it is unreachable
    {
        int i = 1;//unreachable
        if (quarterPrice > fullPrice)//unreachable
        {

        }
    }
}

This is frustrating because I have nor idea why it's wrong, or what to do to fix it. It doesn't give me the red error underline, only the green suggestion line. However, when compiled, none of the code inside of the if statement executes.

I even tried to do:

private void btnFindMax_Click(object sender, EventArgs e)
{
    if (quarterPrice < halfPrice)
    {
       Close();
    }
}

And the code still didn't execute. I have no idea what is going on..

maccettura
  • 10,514
  • 3
  • 28
  • 35
mkamin15
  • 37
  • 4
  • 8
    You have declared the `quarterPrice` and `halfPrice` variables as `const` which means they will never change. In what dimension would `4.50` be greater than `7.50`? – maccettura Feb 15 '18 at 15:31
  • you have constant values. constant values are *constant*, meaning not changing. It's telling you quarterPrice is always lower than halfPrice. – oppassum Feb 15 '18 at 15:31
  • I understand they'll never change, but shouldn't the code still execute? i couldn't even get the `Close()` function to work, even when im asking if 4.50 is less than 7.50 – mkamin15 Feb 15 '18 at 15:34
  • 2
    @MatthewKaminski no, why would it? An `if` statement will only run with a true expression. Will 4.50 ever be larger than 7.50? No – maccettura Feb 15 '18 at 15:35
  • Also, I [know for a fact](https://dotnetfiddle.net/6LhUyK) your second code example (`quarterPrice < halfPrice`) will run. – maccettura Feb 15 '18 at 15:35
  • 1
    Why the down votes? He provided code, it's reproducible. Everyone has to start somewhere. – Derrick Moeller Feb 15 '18 at 15:44

2 Answers2

2

You have defined quarterPrice and halfPrice as constants. The compiler knows that quarterPrice will never be greater than halfPrice and is providing you with a warning.

For example you can generate the same warning like this.

if (false)
{
    int i = 1;

    // Do other work.
}
Derrick Moeller
  • 4,808
  • 2
  • 22
  • 48
2

You have defined the variables as constants. The compiler knows that the condition in your if statement will never be true.

Steve Harris
  • 5,014
  • 1
  • 10
  • 25