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, which evaluate to true or false. It highlights the use of '==' for equality and distinguishes true and false as bool types, separate from strings. Other operators include '!=' for inequality and '>', '<', '>=', '<=' for comparisons. It clarifies that in Python, '=' is for assignment, while '==' is for comparison, cautioning against confusing the two, as there are no symbols for '=<' or '=>'.
    • 3.2: Logical Operators
      This page discusses three logical operators: "and," "or," and "not," explaining their semantics as they relate to natural language. For example, "and" requires both conditions to be true, while "or" needs just one. The "not" operator inverses a boolean expression. It highlights Python's allowance for nonzero numbers to represent "true," which may complicate logical evaluations, urging caution in their use.
    • 3.3: Conditional Execution
      This page explains the importance of conditional statements in programming, focusing on the 'if' statement, which facilitates decision-making based on conditions. It details the syntax, including the condition following the 'if' keyword and the execution of an indented code block if the condition is true. The use of 'pass' as a placeholder for no action is also mentioned, along with the indication of an active code block in the Python interpreter.
    • 3.4: Alternative Execution
      This page discusses an alternative execution form of the if statement in programming, illustrating how a condition leads to one of two outcomes. It provides an example where a variable x determines if a message indicating it is even is printed, or a different message if it is odd. The text emphasizes that only one branch executes based on the truth value of the condition.
    • 3.5: Chained Conditionals
      This page explains chained conditionals in programming using the "if-elif-else" structure in Python. It defines "elif" as "else if," allowing for multiple logic branches where conditions are checked sequentially. The first true condition's branch executes, and the text includes an example of evaluating user input with multiple conditions leading to different outputs.
    • 3.6: Nested Conditionals
      This page explains nested conditional statements in programming, showing how they can be used for complex logic but may reduce readability. It suggests using logical operators like 'and' to simplify these structures. An example illustrates replacing two nested conditionals with one using 'and', achieving the same functionality with improved clarity.
    • 3.7: Catching exceptions Using Try and Except
      This page covers error handling in Python using the "try / except" structure to manage invalid user input that may cause crashes, exemplified by a temperature converter script. It demonstrates modifying the script to include a try block for potentially problematic code, accompanied by an except block to manage errors gracefully. When an exception occurs, a user-friendly error message is displayed, ensuring the program can continue or terminate smoothly.
    • 3.8: Short-Circuit Evaluation of Logical Expressions
      This page explains Python's evaluation of logical expressions using short-circuiting, where evaluations stop if an early part determines the result. This technique helps avoid errors, like division by zero, by using conditionals (guards) to check conditions before executing potentially problematic code. For instance, checking if `y` is non-zero before division prevents runtime errors, demonstrating how the guardian pattern effectively controls evaluation flow and enhances code safety.
    • 3.9: Debugging
      This page explains that Python error tracebacks are crucial for identifying errors and their locations. Syntax errors are easy to understand, but whitespace errors can be tricky due to invisible characters. Indentation errors may point to an incorrect line in the error message, obscuring the real issue. Thus, while error messages indicate where problems are detected, they may not accurately reveal the root cause.
    • 3.E: Conditional Execution (Exercises)
      This page discusses three programming exercises on employee pay and grade evaluation: 1) calculating pay with overtime for hours over 40 hours, 2) handling non-numeric inputs in the pay program for user feedback, and 3) assigning letter grades based on scores between 0.0 and 1.0 while managing invalid inputs.
    • 3.G: Conditional Execution (Glossary)
      This page defines key programming concepts related to conditional statements and boolean logic, including compound statements, boolean expressions, branches, and operators. It covers various types of conditional statements like nested conditionals and introduces concepts such as the guardian pattern and short-circuit evaluation. The page also discusses traceback, which details the executing functions during an exception.


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