Skip to main content
Engineering LibreTexts

4.2: Selection Control Structures

  • Page ID
    10655
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Overview

    In selection control structures, conditional statements are features of a programming language which perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false.[1]

    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
    
    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

    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.[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

    if then else
    A two-way selection control structure.
    case
    A multi-way selection control structure.

    References


    1. Wikipedia: Conditional (computer programming)
    2. Wikipedia: Conditional (computer programming)

    4.2: Selection Control Structures is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?