# 3.5 `if` Statement¶

- Pseudocode: Suppose that a passing grade on an examination is 60. The pseudocode

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

- If the condition is true, 'Passed' is displayed. Then, the next pseudocode statement in order is “performed.”
- If the condition is false, nothing is displayed, and the next pseudocode statement is “performed.”
- Indentation emphasizes that 'Passed' is displayed only if the condition is true.

Corresponding `if` Statement

In [1]:

    grade = 85

In [2]:

    if grade >= 60:
        print('Passed')

    Passed

### Suite Indentation¶

- Indenting a suite is required.

In [3]:

    if grade >= 60:
    print('Passed')  # statement is not indented properly

      File "<ipython-input-3-eb5359d7857b>", line 2
        print('Passed')  # statement is not indented properly
            ^
    IndentationError: expected an indented block

- Statements in a suite must have the same indentation.

In [5]:

    if grade >= 60:
        print('Passed')
      print('Good job!)

      File "<tokenize>", line 3
        print('Good job!)
        ^
    IndentationError: unindent does not match any outer indentation level

### `if` Statement Flowchart¶

- The decision (diamond) symbol contains a condition that can be either `True` or `False`.
- Two flowlines emerging from it:
  - One indicates the direction to follow when the condition in the symbol is `True`.
  - The other indicates the direction to follow when the condition is `False`.

### Every Expression Can Be Treated as `True` or `False`¶

In [6]:

    if 1:
        print('Nonzero values are true, so this will print')

    Nonzero values are true, so this will print

In [7]:

    if 0:
        print('Zero is false, so this will not print')

### An Additional Note on Confusing `==` and `=`¶

- Using `==` instead of `=` in an assignment statement can lead to subtle problems.
- Writing `grade == 85` when we intend to define a variable with `grade = 85` would cause a `NameError`.
- Logic error: If `grade` had been defined before the preceding statement, then `grade == 85` would evaluate to `True` or `False`, depending on `grade`’s value, and not perform the intended assignment.

©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.