Selection Statements
C has two types of selection statements: the if statement and the switch statement.
The if statement is in the form:
if (In the if statement, if the
The switch statement causes control to be transferred to one of several statements depending on the value of an expression, which must have integral type. The substatement controlled by a switch is typically compound. Any statement within the substatement may be labeled with one or more case labels, which consist of the keyword case followed by a constant expression and then a colon (:). The syntax is as follows:
switch (No two of the case constants associated with the same switch may have the same value. There may be at most one default label associated with a switch. If none of the case labels are equal to the expression in the parentheses following switch, control passes to the default label or, if there is no default label, execution resumes just beyond the entire construct.
Switches may be nested; a case or default label is associated with the innermost switch that contains it. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a break; statement is encountered. Fall-through is useful in some circumstances, but is usually not desired. In the preceding example, if
It is possible, although unusual, to insert the switch labels into the sub-blocks of other control structures. Examples of this include Duff's device and Simon Tatham's implementation of coroutines in Putty.
Read more about this topic: C Syntax, Control Structures
Famous quotes containing the words selection and/or statements:
“The books for young people say a great deal about the selection of Friends; it is because they really have nothing to say about Friends. They mean associates and confidants merely.”
—Henry David Thoreau (18171862)
“There is a certain embarrassment about being a storyteller in these times when stories are considered not quite as satisfying as statements and statements not quite as satisfying as statistics; but in the long run, a people is known, not by its statements or its statistics, but by the stories it tells.”
—Flannery OConnor (19251964)