Jeff Schwab said:
I would like this if I knew a suitable jump-table would be generated
in the code, but for such disparate values... Anyway, I actually
*did* like Mike's suggestion of creating an array of boolean values:
if( boolean_array[val] == true )
// Common code.
Even assuming you have an appropriate definition for "true", the
comparison is unnecesary. In general, explicitly comparing boolean
values to "true" or "false" is unnecessary and potentially dangerous.
It's already a boolean, after all.
C (unless you have C99) has no predefined boolean type. In both C and
C++, any non-zero integer value is considered true in a condition; if
your "true" is defined as 1, the comparison will fail for any value
other than 0 or 1.
Just use
if ( boolean_array[val] ) {
/* blah blah */
}
(If you think that "boolean_array[val] == true" is clearer, you should
think that "(boolean_array[val] == true) == true" is even clearer.)
Note that this argument doesn't apply to non-boolean values used as
conditions. A pointer value, for example, can be used as a condition
(implicitly testing whether it's a null pointer), but many programmers
prefer to make the comparison explicit; "if (p)" and "if (p != NULL)"
are equally valid.
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <
http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <
http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)