By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,466 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,466 IT Pros & Developers. It's quick & easy.

nesting switch statements

P: n/a
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.
Aug 8 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Aug 8, 12:46*pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
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
Aug 8 '08 #2

P: n/a
start looking

"jp2msft" <jp*****@discussions.microsoft.comwrote in message
news:28**********************************@microsof t.com...
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.

Aug 8 '08 #3

P: n/a
LOL. Ok.

"gerry" wrote:
start looking
Aug 8 '08 #4

P: n/a
jp2msft <jp*****@discussions.microsoft.comwrote:
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
Aug 8 '08 #5

P: n/a
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.

"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.
Aug 8 '08 #6

P: n/a
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
Aug 8 '08 #7

P: n/a
"Göran Andersson" <gu***@guffa.comwrote:
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.
Aug 8 '08 #8

P: n/a
Paul E Collins <fi******************@CL4.orgwrote:
"Göran Andersson" <gu***@guffa.comwrote:
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...

--
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
Aug 8 '08 #9

P: n/a
Jon Skeet [C# MVP] wrote:
Paul E Collins <fi******************@CL4.orgwrote:
>"Göran Andersson" <gu***@guffa.comwrote:
>>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... :)

--
Göran Andersson
_____
http://www.guffa.com
Aug 9 '08 #10

P: n/a
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.
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
Aug 11 '08 #11

P: n/a
On Aug 11, 4:00*am, Arne Vajhřj <a...@vajhoej.dkwrote:
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.

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

Aug 11 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.