Skip to main content
Engineering LibreTexts

21.3: The if/elif/else statement

  • Page ID
    88708
  • \( \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 problem with the previous example is that we really want our four languages to be mutually exclusive options. If lang is "Spanish", we want it to print “Hola” and skip all the rest. The easiest way to get this behavior is to use “elif” (a horrid abbreviation of the phrase “else if”).

    Operator

    Meaning

    >

    greater than

    <

    less than

    >=

    greater than or equal to

    <=

    less than or equal to

    !=

    not equal to

    ==

    equal to

    and

    and

    or

    or

    not

    not

    Squint hard at this program until you see the differences between it and the previous one:

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

    name = "Javier"

    lang = "French"

    if lang == "Spanish":

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

    elif lang == "French":

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

    elif lang == "Chinese":

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

    else:

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

    It’s identical except that we replaced the second two if’s with elif’s. This tells Python: only if the language is not Spanish should you then consider whether or not it’s French. And only if it’s not French (and not Spanish) should you consider whether or not it’s Chinese. And only if it’s not Chinese (and not French (and not Spanish)) should you print “Hello.” Realize, too, that an entire if/elif/elif/.../elif/else chain is a single statement, no matter how many conditions it has. You can’t just have an “elif” (or an “else,” for that matter) floating out in the void without an initial if to anchor it. This may help you to understand how the elif structure acts, and why it will only ever execute one of the bodies. It’s because:

    Whenever an if/elif/.../elif/else statement is reached, exactly one of the bodies will be executed. It’s never more than one, and it’s never none.

    Whether to use sequential ifs or a chain of elifs isn’t always an easy question to answer. Neither choice is always right: you have to think rigorously logically about how the program should act. Ask yourself: “do I want Python to consider all of these conditions – and execute the appropriate if bodies – no matter what? Or do I want it to bail out as soon as it finds one that’s true?” Like most things, it takes practice to get right.


    This page titled 21.3: The if/elif/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.

    • Was this article helpful?