Skip to main content
Engineering LibreTexts

8.1: Introduction

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

    Our initial examination of the if conditional allowed for the conditional execution of code. A single item (variable) may be tested in the following manner:

    if conditional expression:
         resulting action
    

    If multiple conditions need to be met together, they may be handled with successive tests, as in:

    if conditional expression:
         if conditional expression 2:
              resulting action
    

    This idea can be cascaded on and on. It is important to note, though, that each successive test needs to be indented one more level. Failure to do so will ruin the statement block hierarchy and lead to unpredictable code execution (or syntax errors).

    For example, to see if the variable A is larger than the variable B and is also smaller than the variable C, we might use the following snippet of code:

    if A > B:
         if A < C:
              print( “It’s just right!” )
    

    Note that it is possible to use the else clause for a cascade in order to catch conditions that are not met. For example:

    if A > B:
         if A < C:
              print( “It’s just right!” )
         else:
              print( “A is greater than B but not smaller than C” )
    else:
         print( “A is not greater than B” )
    

    It is extremely important to note the manner of indentation. Note that each else aligns vertically with its parent if.

    So, the above format is applicable if several different conditions must be met simultaneously. An alternate possibility is that at least one condition of several needs to be met. For example, we might define success as the variable A being larger than B or if A is smaller than C, but not necessarily both. Two simple if statements appear to work at first, but there is a problem:

    if A > B:
         print( “Success!” )
    if A < C:
         print( “Success!” )
    

    The problem is that if both conditions are met, Success! will be printed out twice. An else can alleviate this problem:

    if A > B:
         print( “Success!” )
    else:
         if A < C:
              print( “Success!” )
    

    A more compact form uses the elif, which is just a contraction of else and if:

    if A > B:
         print( “Success!” )
    elif A < C:
         print( “Success!” )
    

    This removes the potential double print out but it still leaves a redundant print statement.

    The easiest way around this modest mess is to make use of the logical operators and and or. These are used to make compound tests. The and operator is used to make test which require all parts of the expression to be true while the or operator is used when at least one part needs to be true. Here are the updated versions of the two examples above. Note how compact they are in comparison:

    if A > B and A < C:
         print( “It’s just right!” )
    
    if A > B or A < C:
         print( “Success!” )
    

    It is important to note that any and can be turned into an or by negating the individual tests and the outcome (i.e., reversing their sense) :

    if A <= B or A >= C:
         print( “It’s no longer just right!” )
    
    if A <= B and A >= C:
         print( “Not successful!” )
    

    Finally, multiple and and or operators may be used in a given expression. This may require the use of parentheses.

    if (A > B and A < C) or X != 1:
         print( “Done” )
    

    In the example above, Done is printed if X does not equal 1, regardless of the value of A.


    This page titled 8.1: Introduction is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?