|
JB Enterprises - Johan BezemInterim Management & Consulting |
|
|
JB Enterprises - BlogBizarre switch statement in CIt's been in my "Bizarre C" box for many years. Things you may want or need to know about, but never would want to duplicate. Currently, I am researching for a new publication, maybe a PDF document, maybe a course, on the nooks an crannies of the C programming language. And I'm finding some abstruse samples, I can tell you. But one example I found most bizarre at the time (around 1990), and which still is among my favorites to flabbergast experienced C programmers, I found in the best C programming reference I've known to date: "C - A Reference Manual" by Samuel P. Harbison and Guy L. Steele (see also the book's website). Imagine a calculation depending on certain numbers being prime or not. If the routine gets a prime parameter it executes one routine, if the parameter is not prime, another. Like this:
if (is_prime(a))
process_prime(a);
else
process_nonprime(a);
The if-statement is just that, a statement, a composite statement. A switch-statement is composed of a switch keyword, the value on which to operate, and a, possibly composite, statement. It could be our if-statement, if we so choose. However, without any case/default-label, the switch-statement would just jump over the statement so specified, effectively doing nothing. To remedy that, we put the default-label before the if-statement:
switch (a)
default:
if (is_prime(a))
process_prime(a);
else
process_nonprime(a);
This code already looks strange, but it is functionally equivalent to the
if-statement by itself.
switch (a)
default:
if (is_prime(a))
case 2: case 3: case 5: case 7:
process_prime(a);
else
case 4: case 6: case 8: case 9: case 10:
process_nonprime(a);
To understand why this is correct C, we need to realize that case-labels belonging to a switch
-statement can be positioned anywhere within the boundaries of the
(composite) statement belonging to the switch. In
our case, this means until the semicolon after the
process_nonprime(a) statement.
I can only agree with Sam Harbison and Guy Steele: "This is frankly the
most bizarre switch statement we have ever seen that still has pretenses to
being purposeful." Happy coding! November 3rd, 2009
|