Variables, Operators and Conditionals
Recap From Last Lecture
Upcoming Schedule
Today's Agenda
Variables and Naming Conventions
Data Types and the Assignment Operator
The “=” used here is called the assignment operator and as its name explains, is used to assign values to variables
Arithmetic Operators
Logical and Relational Operators
Why?
→ To keep track of T/F values within a program
→ Booleans take up very little memory
→ To create conditions that can control how a program acts in certain settings
How?
We can utilize logical operators like:
We can also use == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to). These are known as relational operators.
Exercise: Branching
Could there be certain times when a program must act one way if something is true, and another way if not?
Think of one real-world example of a program that would require this
If Statements
If statements may have two more branches, only one of which is executed at a time.
The “if” branch is executed if the condition, and the “else” branch is executed if the condition is false.
If Statements continued
To address this we use “elif” (stands for else if)
For example, a program that sorts items into two boxes that needs to act differently when sorting odd, even, and an empty collection of objects.
Are there examples of a program needing to check more than one conditional in some process?
Individual Exercises