Skip to main content
Engineering LibreTexts

21.4: Nesting

  • Page ID
    88709
  • \( \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}}\)

    Now as if this weren’t complex enough, let me inform you that the body of an if (or else, or elif) statement can itself contain other if statements! This is actually quite common. Consider this example:

    Code \(\PageIndex{1}\) (Python):

    first_name = "Emma"

    last_name = "Watson"

    gender = "female"

    marital_status = "single"

    degree = "BA"

    if degree == "PhD" or degree == "MD":

    print("Why hello, Dr. {}!".format(last_name))

    elif gender == "male":

    print("Why hello, Mr. {}!".format(last_name))

    elif gender == "female":

    if marital_status == "married":

    print("Why hello, Mrs. {}!".format(last_name))

    elif marital_status == "single":

    print("Why hello, Miss {}!".format(last_name))

    else:

    print("Why hello, Ms. {}!".format(last_name))

    else:

    print("Why hello, Mx. {}!".format(last_name))

    Output: Why hello, Miss Watson!

    This program implements the quite convoluted social norms for salutations in Western culture. Consider it carefully. If a person is either a Ph.D. or a Medical Doctor, that trumps everything, and we use “Dr.” as their form of address. This is to indicate just how studly these people are.

    If they don’t have such a college degree – and only if they don’t (notice the elif) – will we then consider their gender. For men, it’s simple: a plain old “Mr.” will do. For women, it’s complicated: their marital status now comes into play. This is the nested part of the structure: we have another if statement (a whole if/elif/else chain, actually) inside the “gender == "female"” condition. If the person in question identifies as neither male nor female, all that Mrs./Miss/Ms. stuff will be skipped, and we’ll drop straight to the Mx. message.

    Pay careful attention to the indentation in these examples, since it’s the key to discerning the structure of the program.


    This page titled 21.4: Nesting is shared under a not declared license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.