Alternatives
- A series of nested if-else conditionals that examine the target one value at a time.
- A lookup table, which contains, as keys, the
case
values and, as values, the part under thecase
statement.
-
- (In some languages, only actual data types are allowed as values in the lookup table. In other languages, it is also possible to assign functions as lookup table values, gaining the same flexibility as a real
switch
statement. See Control table article for more detail on this). - Lua does now support Case/Switch statements: http://lua-users.org/wiki/SwitchStatement . This lookup technique is one way to implement
switch
statements in the Lua language, which has no built-inswitch
. - In some cases, lookup tables are more efficient than non-optimized
switch
statements since many languages can optimize table lookups, whereas switch statements are not optimized unless the range of values is small with few gaps. A non-optimized, non-binary search lookup, however, will almost certainly be slower than either a non-optimized switch or the equivalent multiple if-else statements.
- (In some languages, only actual data types are allowed as values in the lookup table. In other languages, it is also possible to assign functions as lookup table values, gaining the same flexibility as a real
- A control table (that may be implented as a simple lookup table) can also be customized to accommodate multiple conditions on multiple inputs if required and usually exhibits greater 'visual compactness' than an equivalent switch (that can occupy many statements).
- For object-oriented programs, extensive use of polymorphism can be used
Read more about this topic: Switch Statement
Famous quotes containing the word alternatives:
“Clearly, society has a tremendous stake in insisting on a womans natural fitness for the career of mother: the alternatives are all too expensive.”
—Ann Oakley (b. 1944)
“The literal alternatives to [abortion] are suicide, motherhood, and, some would add, madness. Consequently, there is some confusion, discomfort, and cynicism greeting efforts to find or emphasize or identify alternatives to abortion.”
—Connie J. Downey (b. 1934)
“The last alternatives they face
Of face, without the life to save,
Being from all salvation weaned
A stag charged both at heel and head:
Who would come back is turned a fiend
Instructed by the fiery dead.”
—Allen Tate (18991979)