Skip to main content
Engineering LibreTexts

21.2: The if/else Statement

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

    The above examples execute the if body as long as the condition is true, and do nothing otherwise. It’s common to want to do something else in the “otherwise” case instead, and for that, we have the if/else statement.

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

    name = "Gladys"

    cash_on_hand = 2000

    IQ = 120

    print("Nice to meet you, {}!".format(name))

    if cash_on_hand > 5000:

    print("Wow, you're rich! Gimme a fiver.")

    cash_on_hand = cash_on_hand - 5

    else:

    print("I wish you well!")

    if IQ > 100:

    print("Wow, you're smart! Read a book.")

    IQ = IQ + 5

    else:

    print("You're currently not that smart. Read a book!")

    IQ = IQ + 10

    print("{}'s IQ is {} and she has ${}.".format(name, IQ, cash_on_hand))

    | Nice to meet you, Gladys!

    | I wish you well!

    | Wow, you're smart! Read a book.

    | Gladys's IQ is 125 and she has $2000.

    You can see that “I wish you well!” was printed. This is because cash_on_hand was not greater than 5000 (as required by the if condition). Also, the “. . . you’re smart!. . . ” message was printed but not the “. . . not that smart. . . ” one. Both the if part and the else part have an indented body, although only the if part has a condition.

    And that brings up another point. Although it hardly seems worth mentioning, let me nevertheless emphasize this oft-overlooked truth:

    Whenever an if/else statement is reached, either the if body or the else body will always be executed. It’s never both, and it’s never neither one.

    This is always, always true, because of the nature of things. The reason the else header doesn’t have a condition is because its condition is implicitly the exact opposite of the if condition. Period. In any case where the if condition isn’t true – and only in such a case – will the else condition be executed.

    To test whether you fully understand this point, see if you can predict the output of the following program, which 99% of beginning programmers get wrong:

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

    name = "Javier"

    lang = "French"

    if lang == "Spanish":

    print("Hola, {}!".format(name))

    if lang == "French":

    print("Bonjour, {}!".format(name))

    if lang == "Chinese":

    print("Ni hao, {}!".format(name))

    else:

    print("Hello, {}!".format(name))

    Seriously, don’t feel bad if you miss this one. The answer (*drum roll please*) is:

    | Bonjour, Javier!

    | Hello, Javier!

    Wait...wut? Why did it print two messages? Surely if Javier’s preferred language is French, it ought to say “Bonjour” and skip all the other options?

    To understand this behavior, you have to realize that an if/else statement is a single entity. This multi-lingual greeting program has three components:

    1. an if statement
    2. an if statement
    3. an if/else statement

    And you must remember our golden rule in the shaded box: either the if body or the else body will always be executed: never both, and never neither one. Therefore, the above program does this:

    1. If the language is Spanish, print an “Hola” message. (Otherwise, do nothing.)
    2. If the language is French, print a “Bonjour” message. (Otherwise, do nothing.)
    3. If the language is Chinese print a “Ni hao” message. Otherwise, print a “Hello” message.

    Once you recognize that structure, you’ll realize that when step 3 is encountered, the program must print either “Ni hao” or “Hello.” It can’t print both, and it can’t print neither. An if/else just doesn’t work any other way.


    This page titled 21.2: The if/else Statement 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.