sa*****@yahoo.co.in wrote:
(almost topical)
I read somewhere that there are some compile time operations behind
switch-case,
Yes. Code-generation happens at compile-time.
which is why it can work for cases which evaluates to an
integer or character and not strings
No. That's just an efficiency-oriented language-design issue.
and that it makes switch-case faster than if-else statements,
What (can) make switch-case faster than if-then chains is that all
the comparisions (can be) made in one go rather than an if at a
time.
is it true and if so what is the underlying concept behind a
switch-case compilation?
A switch-case where the case labels are dense in the range they
cover (so something like 1000..1020 rather than 1, 17, 32,
29, 123, 256, 501, ...) can often be implemented efficiently
by using an indexed jump instruction.
So the compiler (at compile-time) looks to see if that would be
a good way to implement the switch and, if it is, implements
it that way.
Any decent compiler book will discuss this.
--
Chris "by definition ..." Dollin
"He's dead, Jim, but not as we know it." Unsaid /Trek/.