Make smart decisions in code based on conditions using relational and logical operators.
1. The Hook Every morning when you wake up, you make decision branches: - *If it is raining*, take an umbrella. - *Else if it is sunny*, wear sunglasses. - *Else*, walk normally.
How do computers make smart decisions like this?
2. Real World Story At a highway toll gate, traffic splits into separate lanes: - Emergency ambulances pass through the priority lane. - Heavy trucks pass through the weighing lane. - Cars pass through the FastTag lane.
In C programming, if-else statements act as traffic control gates directing code execution down different paths based on conditions!
3. Concept: Relational & Logical Operators
- Relational: == (Equal), != (Not Equal), >, <, >=, <=
- Logical: && (AND - Both true), || (OR - At least one true), ! (NOT).
4. Code Blueprint
#include <stdio.h>int main() { int marks = 85;
if (marks >= 90) { printf("Grade: Outstanding\n"); } else if (marks >= 33) { printf("Status: PASSED\n"); } else { printf("Status: FAILED\n"); } return 0; } ```
5. Mental Model - `if` = First condition check point. - `else if` = Alternative condition check point. - `else` = Default fallback path if no previous condition matched.
6. Common Mistakes
- Using single equals = (assignment) instead of double equals == (comparison) inside if(x == 5).
7. Summary
Today you learned how to give your programs intelligence using if-else decision branching!
C Runner