switch case

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi friends,
Can we pass more than one value in the case statement? if
we can pass, do we have to separate them by ; or , ? I
have tried those. dont' work. Thank you.
 
Hi,

No you cannot but you can do a drop through:

switch (myVal){
case 1:
case 2:
case 3:
//Do stuff for cases 1, 2 and 3
break;
case 4:
//Stuff for 4
break;
default:
break;
}

Cheers,

Steve
 
Hi,

switch(value)
{
case 1: case 2: case3:
.....
break;
case 4:
case 5:
case 6:
.......
break;
default:
.....
break;
}

Keep in mind that you cannot fall thru from a *case* statement if you have a
code after it so

case 4:
//some code
case 5:
//more code
break;

is not possible

what you should do in this case is to use goto operation

case 4:
//some code
goto case 5;
case 5
//some other code
break;
 
Can we pass more than one value in the case statement? if
we can pass, do we have to separate them by ; or , ? I
have tried those. dont' work. Thank you.

No, you can only test one value.
 
Yes, you can use multiple labels:

//test code
int i=3;
switch (i)
{
case 1:
Console.WriteLine("one");
break;
case 2: case 3: case 4:
Console.WriteLine("two, three, four");
break;
default:
Console.WriteLine("none");
break;
}

LC
 
The C# "switch" is a different animal from the VB "Select Case".

Each case value must be a single constant value.
 
Back
Top