Skip to main content
Chemistry LibreTexts

11: Loop Control Structures

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

     

     

    Break Statement

    Stops execution of loop based on a statement

    Predict the output if you input a value less than one

    fw=float(input("Formula Weight of molecue: "))
    
    while fw < 1:
        break
        print(f' This is impossible as {fw} is smaller than hydrogen')
    
    else: 
       print("you may have a real molecule")
    print("you are out of loop")
    Hello world!

    What do you think will happen if you remove the break?  Note: you can click restart and run all to stop an infinite loop (or ctrl C in thonny)

     

    Continue Statement

    Skips current iteration and moves onto next

    number=(input("Input a number larger than 10: "))
    print('This for loop will use the "continue" control structure to print all nonzero digits in the string')
    for value in number:
        if value=="0":
            continue
        print(value)
    Hello world!

     

     

    Pass Statement

    stops loop from running

    fw=float(input("Formula Weight of molecue: "))
    while fw < 1:
        #While writing code you may want to use a pass statement while you figure things out.
        #without the pass you would go into an infinite loop
        pass
    print("Out of loop")
    Hello world!

     


    This page titled 11: Loop Control Structures is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?