Topic: case statement

topics > computer science > programming > program control > Group: conditional control

conditional control by guards
decision table
if-then-else test chain
else clause


Case statements are a common generalization of selection. They are more readable than test chains or if-then-else statements because each component is independently stated with a unique guard. This readability is reduced when the guards are implicitly stated as in 'execute the i'th' statement, or when the selected statements are goto's. Readability is increased if the guards are positively stated as in 'hard' and 'soft' instead of 'hard' and 'not hard'.

The selector for a case statement may be a variable, a variant type indicator, an event driven loop, an exception generating statement, or a control point dispatcher. The case statement is usually written as a structured statement with explicitly indexed components. The indices, or guards, may be state values, a sub-set of values, a predicate, or a pattern. The indices may be mutually exclusive. If they are not, as in Dijkstra's (QuoteRef: dijkEW_1977) guarded commands, execution is indeterminate.

The result of a case statement may be statement execution, a selected value, or a selected channel for input or output. (cbb 5/80)

Subtopic: as routine with one parameter up

Quote: routine with one parameter is abstraction of case statement; with none, abstraction of assignment [»hehnEC_1977a, OK]
Note: object-oriented programming converts case statements into procedure calls, organized by data [»cbb_1990, OK]

Subtopic: avoid else clause up

Quote: it is better to redundantly restate the else clause; i.e., case statement better than if..then..else [»atkiLV9_1979]

Subtopic: case statements up

Quote: C-- includes an efficient switch statement; e.g., jump table of procedure addresses for a tail call [»joneSP9_1999]
Quote: case statement can be compiled into a jump table, hash table, binary search, sequential search, or if-test-chain [»maclBJ_1987]
Quote: in C, cases are just labels; individual cases are normally terminated by an explicit exit [»ritcDM7_1978c]
Quote: C and BCPL's switch/case construction is very useful; but many other languages did not provide it [»ritcDM7_1978c]
Quote: use a multiway switch to select a route with a number [»wilkMV_1951]
Quote: replaced Algol's switch statement with case statement; mirrors dynamic structure of a program; eliminates extraneous labels [»wirtN6_1966]
Quote: defined case construction for selection by value; no trace of compensating disadvantage [»hoarCA_1974]
QuoteRef: bartCW7_1974 ;;292 Hoare: case labels indicating a range eg Feb...Apr
QuoteRef: bartCW7_1974 ;;293 Wulf: Bliss evaluating case labels; each matching case is executed
Quote: selection by a 'group' of assignment statements; 'G(i)' is a case statement; if..then..else is a 2-element group; 'do..' ignores the indices [»hehnEC_1977]
QuoteRef: kayAC6_1968 ;;39 case statement by 'a[b]' where a<-('statement1', 'statement2' ....)
QuoteRef: kosiPR9_1973 ;;94 INBOUND SWITCH control: input selects which input to send to one output; passes 'done' back to that output
QuoteRef: kosiPR9_1973 ;;94 OUTBOUND SWITCH control: input selects which output to pass data to. passes 'done' back
QuoteRef: cbb_1973 ;; pl/n do case i; statement-list the i'th statement is executed
QuoteRef: weinGM8_1975 ;;41 select first action and select case of, for one action
QuoteRef: wirtN1_1971 ;;52 CASE expression OF > value-of-same-type: statement;
QuoteRef: zahnCT_1974 ;;172 event driven case statement: until ev. or ev2 or... or evn do so then case evb: sl : ev2: s2 etc. see GVBT3
QuoteRef: rainM1_1973 ;;11 can embed case statements in expressions.
QuoteRef: sammJE_1969 ;;434 -label indicates following sub-rules form a table with one literal constituent on left side. allows sorted indexed table
QuoteRef: sammJE_1969 ;;425 rules with labeled subrules (indented with blanks) with control determined by dispatcher

Subtopic: functional definition up

Note: an enum function is a case statement; guarantees a set of functions by object [»cbb_2000, OK]

Subtopic: tabular definition up

Note: use a table for tabular definitions (e.g., case or switch)
[»cbb_1990, OK]

Related Topics up

Topic: conditional control by guards (17 items)
Topic: decision table (29 items)
Topic: if-then-else test chain (18 items)
Topic: else clause
(11 items)

Updated barberCB 4/04
Copyright © 2002-2008 by C. Bradford Barber. All rights reserved.
Thesa is a trademark of C. Bradford Barber.