Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

nesting switch statements

0 views
Skip to first unread message

jp2msft

unread,
Aug 8, 2008, 12:46:00 PM8/8/08
to
Can switch statements be nested?

I've got a large routine that runs off of a switch statement.

If one of the switches in switch #1 is true, that enters switch statement #2.

Some of the statements in switch #2 enter a 3rd switch.

I don't receive any compile errors except whenever I attempt to add default
switches to switches 2 or 3.

Am I nesting too deep, or do I need to call the deeply nested switches in a
special way?

If this is all fine and dandy in C#, then I need to look harder for errors
in my code.

Ignacio Machin ( .NET/ C# MVP )

unread,
Aug 8, 2008, 12:56:18 PM8/8/08
to

Hi,

I would refactor that in such a way that each switch case call a
method. It makes the code more simple to read

gerry

unread,
Aug 8, 2008, 12:53:50 PM8/8/08
to
start looking

"jp2msft" <jp2...@discussions.microsoft.com> wrote in message
news:281B645A-26E7-41F8...@microsoft.com...

jp2msft

unread,
Aug 8, 2008, 1:20:02 PM8/8/08
to
LOL. Ok.

"gerry" wrote:

> start looking
>

Jon Skeet [C# MVP]

unread,
Aug 8, 2008, 1:21:49 PM8/8/08
to
jp2msft <jp2...@discussions.microsoft.com> wrote:
> Can switch statements be nested?

Yes, but that rarely results in readable code. As Ignacio suggested,
separate the nested switch statements into separate methods.

--
Jon Skeet - <sk...@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

jp2msft

unread,
Aug 8, 2008, 1:28:00 PM8/8/08
to
Yeap. My code!

FYI: You've got to have a "break;" listed after the default case if you are
nesting multiple cases.

You just have a single case statement? Probably don't have to stick a break
after the default case.

Göran Andersson

unread,
Aug 8, 2008, 4:52:20 PM8/8/08
to
jp2msft wrote:
> Yeap. My code!
>
> FYI: You've got to have a "break;" listed after the default case if you are
> nesting multiple cases.

You always have to exit from the code following a case label, including
default and including the last one. That can be done in three ways:

1. Use break to exit out of the switch.
2. Use return to exit out of the method.
3. Use goto to jump into the code of another label.

However, if you don't have any code after the case label, you can skip
into the code of next case label.

Example:

swtich (code) {
case 1:
// no code here, and no break, so it skips into case 2
case 2:
Console.WriteLine("1 or 2");
break; // break needed
case 3:
break; // does not skip into the next case
default:
Consolse.WriteLine("other than 1, 2 or 3");
break; // break needed
}

> You just have a single case statement? Probably don't have to stick a break
> after the default case.
>
> "jp2msft" wrote:
>
>> Can switch statements be nested?
>>
>> I've got a large routine that runs off of a switch statement.
>>
>> If one of the switches in switch #1 is true, that enters switch statement #2.
>>
>> Some of the statements in switch #2 enter a 3rd switch.
>>
>> I don't receive any compile errors except whenever I attempt to add default
>> switches to switches 2 or 3.
>>
>> Am I nesting too deep, or do I need to call the deeply nested switches in a
>> special way?
>>
>> If this is all fine and dandy in C#, then I need to look harder for errors
>> in my code.


--
Göran Andersson
_____
http://www.guffa.com

Paul E Collins

unread,
Aug 8, 2008, 6:11:10 PM8/8/08
to
"Göran Andersson" <gu...@guffa.com> wrote:

> You always have to exit from the code following a case label, including
> default and including the last one. That can be done in three ways:
>
> 1. Use break to exit out of the switch.
> 2. Use return to exit out of the method.
> 3. Use goto to jump into the code of another label.

4. Throw an exception.

Eq.


Jon Skeet [C# MVP]

unread,
Aug 8, 2008, 6:24:35 PM8/8/08
to

5. Don't actually have an exit from the code following a case label -
just don't make the code at the end of the case reachable. For
instance, this is legal:

switch (i)
{
case 0:
while(true) {}
case 1:
break;
}

Hadn't actually thought about that before. Not really useful, I
guess...

Göran Andersson

unread,
Aug 9, 2008, 3:31:58 AM8/9/08
to
Jon Skeet [C# MVP] wrote:
> Paul E Collins <find_my_re...@CL4.org> wrote:
>> "Göran Andersson" <gu...@guffa.com> wrote:
>>
>>> You always have to exit from the code following a case label, including
>>> default and including the last one. That can be done in three ways:
>>>
>>> 1. Use break to exit out of the switch.
>>> 2. Use return to exit out of the method.
>>> 3. Use goto to jump into the code of another label.
>> 4. Throw an exception.
>
> 5. Don't actually have an exit from the code following a case label -
> just don't make the code at the end of the case reachable. For
> instance, this is legal:
>
> switch (i)
> {
> case 0:
> while(true) {}
> case 1:
> break;
> }
>
> Hadn't actually thought about that before. Not really useful, I
> guess...
>

Right. I didn't think of alternative 4, although I use it on a regular
basis. It's useful for handling enum values that should not exist:

switch (someEnumValue) {
case SomeEnum.This: DoSomething(); break;
case SomeEnum.That: DoSomethingElse(); break;
default:
// should never get here
throw new NotImplementedException();
}

I've never used alternative 5, and I can't imagine that I ever will... :)

Arne Vajhøj

unread,
Aug 10, 2008, 8:00:07 PM8/10/08
to

Other have already told you that:
1) you can nest switches
2) you should consider creating methods to make the code more readable

I will just described what I have wished for in 20 years: multi value
switch statements.

To write:

switch(a)
{
case 1:
switch(b)
{
case 1:
Console.WriteLine("a=1 b=1");
break;
case 2:
Console.WriteLine("a=1 b=2");
break;
}
break;
case 2:
switch(b)
{
case 1:
Console.WriteLine("a=2 b=1");
break;
case 2:
Console.WriteLine("a=2 b=2");
break;
}
break;
}

as:

switch([a,b])
{
case [1,1]:
Console.WriteLine("a=1 b=1");
break;
case [1,2]:
Console.WriteLine("a=1 b=2");
break;
case [2,1]:
Console.WriteLine("a=2 b=1");
break;
case [2,2]:
Console.WriteLine("a=2 b=2");
break;
}

Arne

Pavel Minaev

unread,
Aug 11, 2008, 2:47:03 AM8/11/08
to

You know, what you actually want is called "pattern matching". Try F#
some day:

match (a, b) with
| (1, 1) -> Console.WriteLine("a=1 and b=1")
| (1, 2) -> Console.WriteLine("a=1 and b=2")
| (0, _) | (_,0) -> Console.WriteLine("a=0 or b=0")
| (x, y) when (x = y+1) -> Console.WriteLine("a = b + 1")
...

0 new messages