Course: CSCI 1250

The if Statement

  • Control structure → directs the order of statement execution
    • Sequence structure → statements executed in the order they appear
    • Decision/Selection structure → executes actions based on a condition

Flowchart Representation

flowchart TD
    A[Start] --> B{Condition}
    B -- True --> C[...]
    B -- False --> D[...]

Python Syntax

if condition:
    # statement(s)

Boolean Variables

  • References one of two values: True or False
  • Represented by the bool type in Python
  • Commonly used for flags, a variable to determine if a condition exists

Boolean Expressions

  • Tested by the if statement to determine truthiness
    • IE: a > b → True if a is greater than b, False otherwise

Relational Operators

  • > → greater than
  • < → less than
  • >= → greater than or equal to
  • <= → less than or equal to
  • == → equal to
  • != → not equal to

The not Operator

  • Reverses the logical value of a boolean expression
    • IE: not False → True (and vise-versa)

Checking Numeric Ranges

  • To check within a range, use and
    • IE: x >= 18 and x <= 21
  • To check outside a range, use or
    • IE: x < 10 or x > 20