1

I've never used switch case instead of if/else if, and I'm wondering how to use it. I would really appreciate the help! The task is to put in an amount of wind in a textbox(tbVindstyrke) and the code should tell the user what amount of Watt per hour(W/t) the wind is generating, in a windmill. It should post the result in a label (lbWattprodusert).

I have got it to work with an if-statement, put as I have understood, this takes up a lot of the computers processioning power (or something). So, I would like to switch it up to a switch-statement.

double Vs = 0;
private void btSjekkW_Click(object sender, EventArgs e)
{
    Vs = Convert.ToDouble(tbVindstyrke.Text);
    if (Vs >= 0 && Vs <= 2.4)
        lbWattProdusert.Text = 0 + " W/t";
    else if (Vs >= 2.5 && Vs <= 3.3)
        lbWattProdusert.Text = 2 + " W/t";
    else if (Vs >= 3.4 && Vs <= 5.4)
        lbWattProdusert.Text = 10 + " W/t";
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
pandapung
  • 11
  • 1
  • 4
    a switch won't work here since you are looking for a value in a range. – Daniel A. White May 08 '19 at 19:07
  • 1
    A quick search and I found this https://stackoverflow.com/a/44078916/5569172 – Rob Rader May 08 '19 at 19:12
  • Possible duplicate of [Switch case: can I use a range instead of a one number](https://stackoverflow.com/questions/20147879/switch-case-can-i-use-a-range-instead-of-a-one-number) – wimh May 08 '19 at 19:14
  • Don't be afraid of the if-statement. One thing to note: you don't need to check the lower limit again, when you have checked it as upper limit just before. Plus 2.45 doesn't fall between the cracks – Hans Kesting May 08 '19 at 19:19
  • If you still want to use ```if/else if```, I recommend that you change it to this ```if (Vs >= 0 && Vs <= 2.4) lbWattProdusert.Text = 0 + " W/t"; else if (Vs <= 3.3) lbWattProdusert.Text = 2 + " W/t"; else if (Vs <= 5.4) lbWattProdusert.Text = 10 + " W/t";``` to avoid gaps between 2.4 and 2.5, and 3.3 and 3.4. – AbdelAziz AbdelLatef Sep 04 '19 at 14:36

3 Answers3

2

switch statements work with constant values.

So, this is valid:

var val = 2;
switch (val)
{
    case 1:
        // Do something if val is 1.
        break;
    case 2:
        // Do something if val is 2.
        break;
    default:
        // Do something for all values of val other than 1 or 2.
        break;
}

But you want to convert an if-else that deals with ranges. That too with double ranges. This is not possible since a switch doesn't allow you to work with a range.

If your ranges are int, you could hypothetically write a case for each value in the rage, but that makes absolutely no sense.

Say, you want to do something if the value is between int 1-3, and something else if it's between 4-6. You could write something like the follwing, but that would be nonsensicle. You'd be better off sticking to an if-else.

var val = 2;
switch (val)
{
    case 1:
    case 2:
    case 3:
        // Do something if val is between 1-3.
        break;
    case 4:
    case 5:
    case 6:
        // Do something if val is between 4-6.
        break;
    default:
        // Do something for all other values of val
        break;
}
Sach
  • 10,091
  • 8
  • 47
  • 84
0

A switch statement is useful when you want to choose between many options based on a single value. When you want to work against ranges, as you have done in your example, an if-statement is needed.

Here's an example of a switch statement... you can see that you specify the value to switch on at the start and you can then perform different actions based on equality.

string color = "red";

switch (color)
{
    case "red":
        // do something
        break;
    case "green":
        // do something
        break;
    case "blue":
        // do something
        break;
    default:
        throw new ColorUnknownException(color);
}

Very often, a switch-statement is a sign of a missing design pattern... but that is a conversation for another day.

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

switch doesn't work with float and double values. However, as all your range limits are multiples of 0.3, you can use the trick of dividing the value by 0.3 to achieve what you seek as follows:

int Vi;
double Vs = 0;
private void btSjekkW_Click(object sender, EventArgs e)
{
    Vs = Convert.ToDouble(tbVindstyrke.Text);
    Vi = (int)(Vs / 0.3);
    switch (caseSwitch)
    {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
            lbWattProdusert.Text = 0 + " W/t";
            break;
        case 9:
        case 10:
        case 11:
            lbWattProdusert.Text = 2 + " W/t";
            break;
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
            lbWattProdusert.Text = 10 + " W/t";
          default:
            // what to do if Vs > 5.4
            break;
      }
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52