3.6 ifelse and ifelifelse Statements

  • Performs different suites, based on whether a condition is True or False.
  • Pseudocode:

If student’s grade is greater than or equal to 60
      Display 'Passed'
Else
      Display 'Failed'

  • Correspondong Python code with variable grade initalized to 85
In [1]:
grade = 85
In [2]:
if grade >= 60: 
    print('Passed')
else:
    print('Failed')
Passed
  • Assign 57 to grade, then shows the ifelse statement again to demonstrate that only the else suite executes
In [3]:
grade = 57
In [4]:
if grade >= 60: 
    print('Passed')
else:
    print('Failed')
Failed

In IPython:

  • The up and down arrow keys navigate backwards and forwards through the current interactive session’s snippets.
  • Pressing Enter re-executes the snippet that’s displayed.
  • In JupyterLab, you can select a cell in its left margin, press C to copy it and V to paste it below the currently selected cell.
In [5]:
grade = 99
In [6]:
if grade >= 60: 
    print('Passed')
else:
    print('Failed')
Passed

ifelse Statement Flowchart

Flowchart of the if…else statement’s flow of control.

Conditional Expressions

  • Sometimes the suites in an ifelse statement assign different values to a variable, based on a condition
In [7]:
grade = 87
In [8]:
if grade >= 60:
    result = 'Passed'
else:
    result = 'Failed'
In [9]:
result
Out[9]:
'Passed'
  • Can write statements like this using a concise conditional expression.
  • The parentheses are not required, but they make it clear that the statement assigns the conditional expression’s value to result.
In [10]:
result = ('Passed' if grade >= 60 else 'Failed')
In [11]:
result
Out[11]:
'Passed'
  • In interactive mode, you also can evaluate the conditional expression directly.
In [12]:
'Passed' if grade >= 60 else 'Failed'
Out[12]:
'Passed'

Multiple Statements in a Suite

In [13]:
grade = 49
In [14]:
if grade >= 60:
    print('Passed')
else:
    print('Failed')
    print('You must take this course again')
Failed
You must take this course again
  • If you do not indent the second print, then it’s not in the else’s suite.
  • In that case the statement always executes, creating strange incorrect output.
In [15]:
grade = 100
In [16]:
if grade >= 60:
    print('Passed')
else:
    print('Failed')
print('You must take this course again')
Passed
You must take this course again

ifelifelse Statement

  • Can test for many cases.
  • Only the action for the first True condition executes.
In [17]:
grade = 77
In [18]:
if grade >= 90:
    print('A')
elif grade >= 80:
    print('B')
elif grade >= 70:
    print('C')
elif grade >= 60:
    print('D')
else:
    print('F')
C

ifelifelse Statement Flowchart

Flowchart of the if…`elif`…else statement’s flow of control.

else Is Optional

  • Handle values that do not satisfy any of the conditions.
  • Without an else, if no conditions are True, the program does not execute any of the statement’s suites.

Logic Errors

  • For a nonfatal logic error, code executes, but produces incorrect results.
  • For a fatal logic error in a script, an exception occurs, Python displays a traceback, then the script terminates.
  • A fatal error in interactive mode terminates the current snippet, then IPython waits for your next input.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 3 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.