473,225 Members | 1,229 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

nesting switch statements

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
11 4913
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
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
LOL. Ok.

"gerry" wrote:
start looking
Aug 8 '08 #4
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
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
13
by: Philip WATTS | last post by:
I am trying to carry out multiple checks on some input data. I am doing this by the running the data through a number of functions. i.e. I have an onclick that calls a function, which in turn calls...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
10
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
22
by: Technoid | last post by:
Is it possible to have a conditional if structure nested inside a conditional switch structure? switch(freq) { case 1: CASENAME if (variable==1) { do some code }
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.