Programming Fundamentals/Selection Control Structures
Overview
editIn selection control structures, conditional statements perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false.[1]
Discussion
editThe 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
editThe if then else control structure is a two-way selection.
If age > 17 Output "You can vote." False: Output "You can't vote." End
Language | Reserved Words |
---|---|
C++ | if , else
|
C# | if , else
|
Java | if , else
|
JavaScript | if , else
|
Python | if , elif , else
|
Swift | if , else
|
Case Control Structure
editThe 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.[2]
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
Language | Reserved Words |
---|---|
C++ | switch , case , break , default
|
C# | switch , case , break , default
|
Java | switch , case , break , default
|
JavaScript | switch , case , break , default
|
Python | N/A |
Swift | switch , 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
edit- if then else
- A two-way selection control structure.
- case
- A multi-way selection control structure.