nesting switch statements

J

jp2msft

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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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 ina
special way?

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

Hi,

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

Jon Skeet [C# MVP]

jp2msft said:
Can switch statements be nested?

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

jp2msft

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

Göran Andersson

jp2msft said:
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
}
 
J

Jon Skeet [C# MVP]

Paul E Collins said:
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...
 
G

Göran Andersson

Jon said:
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... :)
 
A

Arne Vajhøj

jp2msft said:
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.

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
 
P

Pavel Minaev

jp2msft said:
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.

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;

}

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")
...
 

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