Skip to main content
Engineering LibreTexts

3: Conditional Execution

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

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    • 3.1: Boolean Expressions
      This page explains boolean expressions in Python, which evaluate to true or false using comparison operators such as ==, !=, >, <, >=, and <=. It emphasizes the distinction between the assignment operator (=) and the equality operator (==), noting that a common error is mistakenly using a single equal sign for comparison. Additionally, it mentions that Python does not recognize operators like =< or =>.
    • 3.2: Logical Operators
      This page explains the logical operators "and," "or," and "not," detailing their meanings and providing examples such as "x > 0 and x < 10" for conjunction and "n%2 == 0 or n%3 == 0" for disjunction. It mentions that "not" negates expressions. While Python expects boolean operands, it treats nonzero numbers as "true," introducing a complexity that can be confusing for beginners.
    • 3.3: Conditional Execution
      This page explains conditional statements in programming, specifically the if statement, which checks conditions to alter program behavior. It details the structure of an if statement, including the requirement for an indented code block that executes if the condition is true. The page also notes the use of the pass statement as a placeholder and mentions that entering an if statement in the Python interpreter changes the prompt, indicating a continuing block of code.
    • 3.4: Alternative Execution
      This page explains the if statement's alternative execution, demonstrating its functionality through a condition that dictates which code block executes. An example checks if a number x is even or odd using an if-else structure, highlighting that one of the two branches runs based on the condition's truth value, underscoring the binary logic inherent in if-then-else programming.
    • 3.5: Chained Conditionals
      This page explains chained conditionals in programming, highlighting the use of if-elif-else structures to evaluate multiple possibilities. The elif statement allows for sequential checks of additional conditions, with multiple elifs possible. If an else clause is present, it must be placed last. Conditions are evaluated in order, leading to action based on the first true condition while ignoring later true conditions, thus aiding in decision-making across different input scenarios.
    • 3.6: Nested Conditionals
      This page discusses nested conditionals in programming, illustrating their complexity through an example involving two variables, x and y. It suggests that logical operators, such as 'and', can simplify the code and improve readability by reducing nested conditions. The text emphasizes the importance of clarity in programming and the challenges posed by excessive nesting in decision-making structures.
    • 3.7: Catching exceptions Using Try and Except
      This page discusses error handling in Python, focusing on managing user input errors that can cause runtime issues. It explains the "try/except" structure for gracefully handling exceptions without crashing. An example of a Fahrenheit to Celsius converter demonstrates how to implement this error handling, providing user-friendly feedback for invalid input.
    • 3.8: Short-Circuit Evaluation of Logical Expressions
      This page discusses Python's evaluation of logical expressions through short-circuiting, which halts evaluations once the truth value is clear. It highlights the guardian pattern, where additional checks prevent errors during execution, such as division by zero. Examples illustrate how avoiding unnecessary evaluations contributes to safer code, helping developers write more robust programs.
    • 3.9: Debugging
      This page discusses Python error handling, emphasizing the importance of tracebacks in identifying error types and their locations. It highlights common issues like syntax errors and the subtlety of whitespace errors, where invisible spaces and tabs can complicate debugging. The summary explains that error messages point to the detection point, which may differ from the actual source of the error in the code.
    • 3.E: Conditional Execution (Exercises)
      This page describes three exercises aimed at enhancing a pay computation program: Exercise 1 focuses on calculating pay with overtime for hours worked beyond 40; Exercise 2 implements error handling for non-numeric input; and Exercise 3 prompts users for scores between 0.0 and 1.0, assigning grades while managing out-of-range inputs and providing error messages. The text concludes with a preview of future topics covering functions and loops.
    • 3.G: Conditional Execution (Glossary)
      This page outlines key concepts in conditional statements and boolean logic in programming, including compound statements, boolean expressions, branches, and operators. It covers the guardian pattern, nested conditionals, traceback, and short-circuit evaluation, all crucial for governing execution flow based on conditions.


    This page titled 3: Conditional Execution is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Chuck Severance via source content that was edited to the style and standards of the LibreTexts platform.