View previous topic :: View next topic |
Author |
Message |
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
nested switch question |
Posted: Tue Sep 12, 2006 6:57 am |
|
|
If I have a sequence of nested switch statements can I break ou of this anywhere using a goto to the end of the function containing the statements without stuffing up the stack?
Code: |
void myfunction()
{
switch (j)
{
case 1: switch (k)
{
case x: switch(l)
{
case y: goto ErrorExit;
...
}
...
}
...
}
ErrorExit:
} |
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
nurquhar
Joined: 05 Aug 2006 Posts: 149 Location: Redditch, UK
|
|
Posted: Tue Sep 12, 2006 7:24 am |
|
|
I think goto should work ok, the switch should all be done with inline stuff (however there is no rule that the compiler must do it this way!).
Check the compiler asm listing that the goto handles any long jumps across banks.
Since many people frown on the use of "goto" you could code the switches inside an #inline function and process a "return" from which ever cases you need.
eg
Code: |
#inline int checkerr(int a, int b)
{
switch (a) {
case 0 :
switch(b) {
case 1 :
return 1 ;
break ;
case 2 :
return 2 ;
break ;
default :
break ;
}
break ;
case 1 :
switch(b) {
case 1 :
return 1 ;
break ;
case 2 :
return 2 ;
break ;
default :
break ;
}
break ;
default :
break ;
}
return 0 ;
}
void func()
{
int err ;
err = checkerr(1,2) ;
}
|
|
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Tue Sep 12, 2006 8:28 am |
|
|
Quote: | Since many people frown on the use of "goto" you could code the switches inside an #inline function and process a "return" from which ever cases you need. |
I am one of those that frown on goto instructions however, in this case, the code is far easier to read and understand the flow instead of trying to work out which level of the case caded switch statements you are "break"ing out of. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Mattr0
Joined: 27 Mar 2005 Posts: 30
|
|
Posted: Tue Sep 12, 2006 1:44 pm |
|
|
I really frown on goto statements how about this
Code: |
void myfunction()
short ex = 0;
{
switch (j)
{
case 1: switch (k)
{
case x: switch(l)
{
case y: ex = 1;
break;
...
}
...
}
...
}
if(ex) {
do whatever
}
} |
When it get to case Y it will break out of both switch statements and chack if you set your flag and then run that code. It's a little cleaner and you don't have to worry about how the goto statement is going to work. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Tue Sep 12, 2006 8:35 pm |
|
|
Quote: | When it get to case Y it will break out of both switch statements |
No it will only break out of the current case. It will then continue executing any statements remaining within the next outer layer and so on. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
|