Skip to main content
Chemistry LibreTexts

2: if Statement

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

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    In an if statement a block of code is executed if a condition is TRUE, and you exit if it is FALSE.  

    if [condition statement]:
        executed action

    Note

    if statement is defined by a block of code which uses a colon and indentation

    clipboard_e475ffd1a5da805ca0fcd58b92a7c10b7.png

    Lets use an if statement to determine if an input is valid, for example, hydrogen is the smallest atom and so a molecule must have a molar mass greater than hydrogen.  The following flow chart describes a program where you input a molar mass and use an if statement to determine if it is acceptable.

    clipboard_ea7ff2587f702a79cda7ffd92a21a14a6.pngFigure \(\PageIndex{1}\): If statement to check if input is valid or not. (Belford CC 0.0)

    The following code performs the action of the above flow chart

    molar_mass=float(input("Enter the molar mass of a molecule in units of g\mol"))
    
    if molar_mass < 2.0:
        print("Your molar mass makes no sense")
        
    print(f"The molar mass you entered is: {molar_mass}g/mol")

    Note how we put a space between the input step, and the print statement that is outside of the loop, and this if statement has two lines of code.

     

    Exercise \(\PageIndex{1}\)

    There is a flow in the logic of the above code  Do you see it? The smallest molecule is H2 and so you coud enter a mass of 1.5g/mol, which is smaller than the smallest molecule.  Can you fix the code so it does not accept 1.5 as an input?

    Answer

    Make the value you are doing the less than equal to 2.

     


    This page titled 2: if Statement is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?