Skip to main content
Engineering LibreTexts

21.1: The if statement

  • Page ID
    83016
  • \( \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 main branching statement in Python and most languages is the if statement. Here’s a couple of them in action:

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

    1: name = "Horace"

    2: cash_on_hand = 100000

    3: IQ = 90

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

    5: if cash_on_hand > 5000:

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

    7: cash_on_hand = cash_on_hand - 5

    8: if IQ > 100:

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

    10: IQ = IQ + 5

    11: print("{}'s IQ is {} and he has ${}.".format(name,

    12: IQ, cash_on_hand))

    Even without any explanation, you might be able to figure out that the output of the code snippet above is:

    | Nice to meet you, Horace!

    | Wow, you're rich! Gimme a fiver.

    | Horace's IQ is 90 and he has $99995.

    If not, stay tuned. Just like a for loop, every if statement has a header and a body. And just like a for loop, the determining factor of which lines constitute the body depends on the indentation:

    • The first if statement’s header is line 5.
    • The first if statement’s body is lines 6 and 7.
    • The second if statement’s header is line 8.
    • The second if statement’s body is lines 9 and 10.

    When an if statement is reached, its condition is evaluated; in the first case, the condition “cash_on_hand > 500” is evaluated to True, and in the second case, “IQ > 100” is determined to be False. Then, only if the condition is true will the body of the if statement execute. Otherwise, it’ll be skipped over.

    Thus, the lines of the above program execute in this order: 1, 2, 3, 4, 5, 6, 7, 8, 11/12. Lines 9 and 10 are skipped entirely, since Horace’s IQ wasn’t above average. Observe that the cash_on_hand variable was updated inside the body of the first if statement, but that IQ was not.

    Compound Conditions

    Conditions can be more complicated than the ones above; just as with queries (p. 128) they can contain more than one component:

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

    if cash_on_hand > 10000 and IQ < 50:

    You might have been surprised to see the word “and” in that if statement instead of the character “&”. I feel you. It’s totally inconsistent, but nevertheless true: although in a query, you must use the symbols &, |, and ~, in an if condition, you must use the words and, or, and not. (In other news, the bananas around the components of an if condition aren’t necessary, but you can include them if you want.)

    For your convenience, the if condition operators are listed in Figure 21.1. (Remember the double-equals!!)


    This page titled 21.1: The if 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?