Skip to main content
Chemistry LibreTexts

1: Introduction to Logic and Control Structures

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

    Control structures allow program to use logic to execute code.  In python the syntax uses blocks of code defined by colons and indentation. There are two basic types, conditionals and loops.  The logic is typically defined by an operator.  These structures can be nested

    Conditionals

    Execute one or more statements if a condition is met.

    • If statement
    • If-Else statement
    • If-Elif-Else statement
    clipboard_e20f45d36ffba146eee4897d661a19ace.pngFigure \(\PageIndex{1}\): Conditional statement logic diagrams. (Belford cc 0.0)

     

    Loops

    Iterate through a statement if a condition is meet

    clipboard_e382a3ec2f82bcd0d439a28a48aaad626.pngFigure \(\PageIndex{2}\): For and While loop flow diagrams. (Belford cc 0.0)

    Boolean vs. Arithmetic Algebra

    We are all familiar with arithmetic algebra and for many students this will be their first foray into Boolean Algebra. In arithmetic algebra you can perform operations on variables, and then apply numerical values to those variables and output numerical answers. In Boolean Algebra the output is a binary "truth value" (True or False) that may be based on a numerical relationship, but may also be based on non-numerical relationships such as identity and membership (look at the operators below). It needs to be realized that the output is the "truth value", and the flow charts above can result in binary decision trees where the output is based on the truth values of the individual Boolean operations (nodes). The success of coding often depends on parsing your problem in a manner that is amenable to Boolean algebraic operations in a manner that results in the behavior of the system you want to create.

    So in summary, arithmetic algebra can apply data to sequential arithmetic operations and come up with a numerical answer, while Boolean algebra can apply data to sequential Boolean operations that results in a binary decision tree based on the truth value of each operation and be used in decision making.

    Operators

    There are a variety of operators that are typically used in logic and control structures.

    Comparison Operators

    See section 2.15: Comparison Operators for examples

    • <
    • <=
    • >
    • >=
    • ==
    • !=

    Logical Operators

    See section 2.16: Logical Operators for examples

    • and
    • or
    • not

    Identity Operators

    See section 2.17: Identity Operators for examples

    • is
    • is not

    Membership Operators

    See section 2.18: Membership Operators for examples

    • in
    • not in

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

    • Was this article helpful?