J
junky_fellow
Which is better using a switch statement or the if-then equivalent
of switch ?
of switch ?
Which is better using a switch statement or the if-then equivalent
of switch ?
Michael said:Define "better".
You can do things with switch statements and fallthrough cases that
are awkward with if/(else if/)else blocks. On the other hand,
switch works only for integers and accepts only constants for the
case labels.
As remarked in another reply: Tell us what you want to achieve
and we can comment in more detail on the pros and contras.
I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.
I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.
Define "better".
You can do things with switch statements and fallthrough cases that
are awkward with if/(else if/)else blocks. On the other hand,
switch works only for integers and accepts only constants for the
case labels.
As remarked in another reply: Tell us what you want to achieve
and we can comment in more detail on the pros and contras.
Which is better using a switch statement or the if-then equivalent
of switch ?
Which is better using a switch statement or the if-then equivalent
of switch ?
Which is better using a switch statement or the if-then equivalent
of switch ?
Thomas Matthews said:Have you tried implementing a "jump table"?
A jump table is a array of <key, function pointer>
records. If the keys are contiguous, the table
becomes an array of function pointers.
If you want to force the compiler to use a jump
table, then make one yourself.
I use if-then-elseif ladders for small quantities.
For bigger quantities, I use a switch statment.
For strings and data driven processes, I use a
jump table. Each has their advantages and
disadvantages; you'll just have to learn when
to use them.
I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.