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

Big Switch!!

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



Joined: 17 Jan 2006
Posts: 66

View user's profile Send private message

Big Switch!!
PostPosted: Mon Oct 23, 2006 4:45 am     Reply with quote

Hello,
I need to use a "switch" structure in which I'll have more than 30 different cases...
Is it recommended doing it as a "big switch-case"??
Any other option??

Thanks,

Juanma (the "big asker") :P
Ttelmah
Guest







PostPosted: Mon Oct 23, 2006 6:37 am     Reply with quote

No reason not to.
There are two different 'versions' of code generated by the switch statement though. If you use a normal 'switch', _with a 'default' statement present_, the code generated is exactly the same as if you used a whole tree of 'if' tests. If instead, you omit the 'default', the compiler will generate a much faster (for the latter tests), and more compact for dealing with a lot of tests, 'jump table'.
So it is more efficient, to have:
Code:

if (val>30) {
   'your default code here'
}
else {
   switch (val) {
   case 0:


   case 1:


......
   
   case 29:


   case 30:
//using all the cases 0..30 for the example given

   }
}


Best Wishes
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