411 REFERENCE · Wikibooks
Programming Fundamentals/Selection Control Structures
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Overview
In selection control structures, conditional statements perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false.Wikipedia: Conditional (computer programming)
Discussion
The basic attribute of a selection control structure is to be able to select between two or more alternate paths. This is described as either two-way selection or multi-way selection. A question using Boolean concepts usually controls which path is selected. All of the paths from a selection control structure join back up at the end of the control structure, before moving on to the next lines of code in a program.
If Then Else Control Structure
The if then else control structure is a two-way selection.
If age > 17
Output "You can vote."
False:
Output "You can't vote."
End
LanguageReserved WordsC++if, elseC#if, elseJavaif, elseJavaScriptif, elsePythonif, elif, elseSwiftif, else
Case Control Structure
The case control structure is a multi-way selection. Case control structures compare a given value with specified constants and take action according to the first expression to match.Wikipedia: Conditional (computer programming)
Case of age
0 to 17 Display "You can't vote."
18 to 64 Display "You're in your working years."
65 + Display "You should be retired."
End
LanguageReserved WordsC++switch, case, break, defaultC#switch, case, break, defaultJavaswitch, case, break, defaultJavaScriptswitch, case, break, defaultPythonN/ASwiftswitch, case, break (optional), default
Python does not support a case control structure. There are workarounds, but they are beyond the scope of this book.
Key Terms
if then else
A two-way selection control structure.
case
A multi-way selection control structure.
References
cnx.org: Programming Fundamentals – A Modular Structured Approach using C++