Easy Question using switch / case

  • Thread starter Thread starter graeme g
  • Start date Start date
G

graeme g

hi

how would i write the following in switch case statement:

if (x < 40)
y = 0;
else if (x < 65)
y = 1;
else if (x < 80)
y = 2;
else if (x < 90)
y = 3;
else if (x < 96)
y = 4;
else
y = 5;

is it easy or not possible?

thanks
g
 
is it easy or not possible?

Not possible.

Best Regards,
Dustin Campbell
Developer Express Inc
 
graeme g said:
hi

how would i write the following in switch case statement:

if (x < 40)
y = 0;
else if (x < 65)
y = 1;
else if (x < 80)
y = 2;
else if (x < 90)
y = 3;
else if (x < 96)
y = 4;
else
y = 5;

It's not possible, I'm afraid. Case statements use equality.

///ark
 
Hmmm.

I couldn't figure out a C# version.


But for kicks, I tried to write a VB.net version, and it worked.



Private Sub SelectCaseTest()



Dim x As Int32 = 11

Dim y As Int32 = 0

Select Case x

Case Is < 10

y = 11

Case Is < 20

y = 21

Case Else

y = 999

End Select

Console.WriteLine(String.Format("{0}, {1}", x, y))



End Sub







Hmmmmmmmmmmmm.

This was all I could come up with, but I didn't try too long: But its not a
solution I know.

private static void SelectCaseTest()

{

int x = 11;

int y = 0;

switch (x)

{

case 1:

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:

case 10:

y = 11;

break;





default:

y=999;

break;

}



Console.WriteLine(string.Format("{0}, {1}", x, y));

}
 
I couldn't figure out a C# version.

I don't think it's possible because the case statement doesn't accept
operators, only absolute values...
But for kicks, I tried to write a VB.net version, and it worked.

Another one to add to the list of differences :-)
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
y = 11;

Not much good if x <= 0 :-)
 
graeme said:
hi

how would i write the following in switch case statement:

if (x < 40)
y = 0;
else if (x < 65)
y = 1;
else if (x < 80)
y = 2;
else if (x < 90)
y = 3;
else if (x < 96)
y = 4;
else
y = 5;

is it easy or not possible?

thanks
g

Hi Graeme,

Unfortunately, switch statements only work across constant values so there's
no easy way to represent those statements. However, if you think about it,
it's more concise using the if..else if statements you have already, since
a case statement *must* include a 'break;', meaning you will be adding an
extra line of code to each condition.
 
graeme g said:
hi

how would i write the following in switch case statement:
[snipped]

For what it's worth (in addition to the good comments you've received
already), it seems to me that what you're doing would be easily (and maybe
more elegantly) handled using an array to define the mapping. For example:

int [] mpivalinum = new int[] {40, 65, 80, 90, 96};
int y;

for (y = 0; y < mpivalinum.Length; y++)
{
if (x < mpivalinum[y])
{
break;
}
}

It has more lines of code for a few numbers, but about the same for the
handful in your example, and a lot less as the number of cases goes up.

Pete
 
Back
Top