CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

nested switch question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

nested switch question
PostPosted: Tue Sep 12, 2006 6:57 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Sep 12, 2006 7:24 am     Reply with quote

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: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Sep 12, 2006 8:28 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Sep 12, 2006 1:44 pm     Reply with quote

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: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Sep 12, 2006 8:35 pm     Reply with quote

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!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group