multiple "case" in a switch

K

Kevin Blount

I've been unsuccessful finding a site that tells me, so I'm asking here:

can I have multiple 'clauses' per 'case' in a 'switch' statement?

e.g.

switch "myVar"
{
case "1", "a":
break;
case "2", "b":
break;
default:
break;
}

and if so, what's the proper syntax, as the above didn't work ;)

Many thanks
 
W

William Stacey [MVP]

string s = "b";
switch (s)
{
case "1":
case "a":
Console.WriteLine("s is 1 or a");
break;
case "2":
case "b":
Console.WriteLine("s is 2 or b");
break;
default:
break;
}

--
William Stacey [MVP]

| I've been unsuccessful finding a site that tells me, so I'm asking here:
|
| can I have multiple 'clauses' per 'case' in a 'switch' statement?
|
| e.g.
|
| switch "myVar"
| {
| case "1", "a":
| break;
| case "2", "b":
| break;
| default:
| break;
| }
|
| and if so, what's the proper syntax, as the above didn't work ;)
|
| Many thanks
 
R

Rickard

Kevin said:
I've been unsuccessful finding a site that tells me, so I'm asking here:

can I have multiple 'clauses' per 'case' in a 'switch' statement?

e.g.

switch "myVar"
{
case "1", "a":
break;
case "2", "b":
break;
default:
break;
}

and if so, what's the proper syntax, as the above didn't work ;)

Many thanks

switch "myVar"
{
case "1":
case "a":
break;
case "2":
case "b":
break;
default:
break;
}
 
R

Richard Whitcher

Kevin,
can I have multiple 'clauses' per 'case' in a 'switch' statement?
Yes..

and if so, what's the proper syntax, as the above didn't work ;)

...the syntax is like this.

switch myVar
{
case "1":
case "a":
// your code here
break;
case "2":
case "b":
// your code here
break;
default:
// your code here
break;
}

Regards,

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top