CS 115: If Statements and Boolean Operations

Mathematical Operations

Basic Operators
  • Addition (+)

    • Example: 15 + 3 = 18

  • Subtraction (-)

    • Example: 15 - 3 = 12

  • Multiplication (*)

    • Example: 15 * 3 = 45

  • Division (/)

    • Example: 15 / 3 = 5

Other Operators
  • Integer Division (//)

    • Example: 5 // 3 = 1

  • Modulus (%)

    • Example: 5 mod 3 = 2

  • Exponentiation (**)

    • Example: 5 ** 3 = 125


Comparison Operations

  • Compares the values on either side; also called relational operators.

Common Operators
  • Greater than (>)

    • Example: 15 > 3 # is True

  • Greater than or equal (>=)

    • Example: 15 >= 3 # is True

  • Less than (<)

    • Example: 15 < 3 # is False

  • Less than or equal (<=)

    • Example: 15 <= 3 # is False

  • Not equal (!=)

    • Example: 15 != 3 # is True

  • Equal (==)

    • Example: 15 == 3 # is False

Examples of Comparison Operators

```python
print(4 > 2) # True
print(7 == 8) # False
print(10 <= 10) # True print(5 != 5) # False print(3 < 1) # False print(9 >= 6) # True

- Additional Variable Examples:  

python
a = 6
b = 10
print(a < b) # True print(a == b) # False print(b >= 10) # True
print(a != 6) # False

---  

## Logical Operators  
- Logical Operators combine or modify boolean expressions, including relational expressions.  
### Types of Logical Operators  
- **And Operator**  
  - Syntax: **expression1 and expression2**  
  - Result: True only if both expressions are True.  
- **Or Operator**  
  - Syntax: **expression1 or expression2**  
  - Result: True if at least one expression is True.  
- **Not Operator**  
  - Syntax: **not expression**  
  - Result: Flips the value (True becomes False, False becomes True).  

### Examples of Logical Operators  

python
a = 10
b = 5
c = 3
print(a > 5 and b < 10) # True print(a < 5 or b == 5) # True print(not (a == 10)) # False print(a < 20 or b > 10) # True
print(not (b <= 5 and c == 3)) # False
print(not (c != 3)) # True

---  

## If Branching (Conditional Statements)  
- A branch is a program path taken based on an expression's value.  
### How If Branching Works  
- **If branching**: The branch is taken if an expression evaluates to True.  

### Visual Representation  
- Flowchart:  
  - Condition Statements  
  - Direction: Start -> Check Condition -> If True -> Execute Statement  

### Example of If Branching  
- Condition: Extra baggage fee is $25 when a suitcase is over 50 lb.  
  - Conditional Statement:     ext{weight} > 50  

### Python Notation for If Branching  
- Syntax:  

python
if condition:
statement(s)

  - Indentation: Must use 4 spaces or a tab and never mix spaces and tabs.  

#### Example: If Branching in Python  

python
if weight > 50:
print(“baggage fee is $25”)

---  

## Exercise: If Branching Implementation  
### Task: Complete the Program  
- Prompt for user's input based on weight:  

python
if weight > 50:
print(“baggage fee is $25”)

### Prompt Implementation  
- Full Solution:  

python
weight = float(input(“Enter bag weight”))
if weight > 50:
print(“baggage fee is $25”)

---  

## Activity 11: If Statements Programming  
### Task: Write a program that prompts an extra baggage fee of $50 when:  
- The baggage is over 50 lb  
- The linear length is over 62 inches.  
### Example Outputs  
- User input: 60 lb & 75 inches should output: 
  - “Extra baggage fee is $50”  
- User input: 50 lb & 65 inches should output:  
  - “Extra baggage fee is $50”  

---  

## If-Else Statements  
- An if-else has two branches:  
  - Branch 1: Taken when the condition is True  
  - Branch 2: Taken when the condition is False  

### Diagram  
- Condition -> Start -> Branch 1 (if True) -> Execute Statements -> Stop  
- Otherwise, go to Branch 2 (if False) -> Execute Statements -> Stop  

### Example of If-Else in Python:  
- Program checks if a number is between 0 and 10  

python
if (0 < number) and (number < 10):
print(“The number is between 0 and 10”)
else:
print(“Number is out of range”)

---  

## Activity 12: If-Else Statements Programming  
### Task: Write a program that prompts an extra baggage fee of $50 when conditions are met, otherwise show no fee.  
### Example Outputs  
- Input: 60 lb & 75 inches outputs:  
  - “Extra baggage fee is $50”  
- Input: 50 lb & 55 inches outputs:  
  - “No extra baggage fee”  

---  

## More on If-Else: Example Scenario  
- Create a program determining if a student has passed based on their final score.  
- If the student passes, add a congratulatory message; otherwise, suggest to study more. 
- Define passing score: 60.  

### Diagram  
- Condition: score > 60, another path if False leads to “You have failed!”  

### Python Code Example  

python
score = float(input(“Enter score: ”))
if score > 60:
print(“You have passed!”)
print(“Congratulations”)
else:
print(“You have failed!”)
print(“Review the material”)

---  

## Multi-branch If-Else Statements  
- This structure can be extended to have three or more branches, with sequences checked.  

### Diagram  
- Condition 1 leads to Branch 1, else proceed to Condition 2 for Branch 2, else to Branch 3.  

### Python Notation for Multi-branch  

python
if condition1:
# branch 1 statement(s)
elif condition2:
# branch 2 statement(s)
else:
# branch 3 statement(s)

---  

## Multi-Branch If-Else: Temperature Program Example  
- Request user to enter the temperature and provide messages based on input.  
### Conditions to Check  
- If temperature > 90, output: "It's a hot day."  
- If temperature > 70, output: "It's a nice day."  
- If temperature > 50, output: "It's a bit chilly."  
- For any temperature below, output: "It's cold outside."  

### Python Code Example  

python
temp = float(input("Enter temperature: "))
if temp > 90:
print("It's a hot day.")
elif temp > 70:
print("It's a nice day.")
elif temp > 50:
print("It's a bit chilly.")
else:
print("It's cold outside.")
```


Activity 13: Multi-Branch If-Else Statements Programming

Task: Prompt user for the final score for CS 115 and display the letter grade based on the score.
Example Outputs
  • Input: Enter score: 70 → “Your final grade is C-”

  • Input: Enter score: 91 → “Your final grade is A-”


Activity 14: Informal Feedback

Task: Complete the informal mid-semester feedback survey and confirm in Piazza activity 14 thread.
  • Note: Must use ODU account; your email will not be recorded by the form.

  • Link: https://forms.gle/GFUL5LUceXJiszWD7