|
|
View previous topic :: View next topic |
Author |
Message |
Neckruin
Joined: 17 Jan 2006 Posts: 66
|
Big Switch!! |
Posted: Mon Oct 23, 2006 4:45 am |
|
|
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
|
|
Posted: Mon Oct 23, 2006 6:37 am |
|
|
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 |
|
|
|
|
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
|