The D Programming Language: switches

Richard Harter g-rh at cca.CCA.COM
Thu Mar 24 13:03:37 AEST 1988


>No, *practically* the switch construct is more powerful because it selects
>in one step.  *Theoretically* switch can compile into an if-then-else chain
>(and often will, if the case values are widely separated), and an
>if-then-else chain can be analysed to discover whether it can be compiled
>into a jump table.

>The theoretical power of a language is [determined by] the set of
>algorithms it can implement.  Its efficiency in executing these algorithms
>depends on its implementation, though it's obviously easier to produce
>efficient implementations of some languages than others.

This is a useless definition of the power of a language.  Almost all
languages of any interest can implement a turing machine emulation in
which all effective algorithms can be implemented.  You can even do it
if the only flow-control construct is the 'while' loop.

You can pick any measure of strength of constructs that you like --
however the usual measure is space and time efficiency.  For example, it
can be shown, that if language A provides the if-then-else and the while-loop
and language B provides the if-then-else and the goto as their sole flow
control constructs, then there are programs of length N and execution time
O(N) in language B such that any equivalent program in language A requires
either O(N logN) execution time or O(n *exp(N)) space, or some combination
thereof.  By this criteria the goto is more powerful than the while-loop.
The hierarchy of strengths runs

	while-loop
	forever-loop-with-multiple-escapes
	goto
	procedure-invocation-and-return
	computed-goto/switch

The switch construct is equivalent in power to the computed goto.

This measure of power is unambiguous.  One can also speak of the expressiveness
of a construct, in the sense of what can be easily done in the language.
Although this is probably the most useful criteria (and I suspect what you
really) meant it is obviously vague and subjective.
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.



More information about the Comp.lang.c mailing list