If Statements and Logic Operators
Lecture Overview
Topics Covered:
- If/Else Statements - Making decisions in code
- Logical Operators - Combining conditions with
and, or, not
- Comparison Operators - Testing relationships between values
- Equality vs Identity - Understanding
== vs is
- Floating Point Comparison - Why floats are tricky
- Nested Conditionals - Complex decision trees
- Input Validation - Checking user input before processing
Part 1: Basic If/Else Statements
Why Do We Need Conditionals?
Programs need to make decisions based on different situations:
- Is a student's mark above 50? → Pass or Fail
- Is a user old enough? → Grant or deny access
- Is it raining? → Take umbrella or not
Conditionals allow your program to execute different code based on whether conditions are true or false.
Basic If Statement Syntax