1 / 20
Session 8: Advanced Conditionals
Either This or That - Advanced Decision Making
Today we level up our decision-making skills!
From basic if statements to intelligent if-else chains
Making smarter, more efficient decisions in code!
Today's Advanced Decision Mission
↔️
Master if-else
Either this OR that logic
🔗
else-if Chains
Multiple conditions elegantly
🎯
Nested if Statements
Decisions within decisions
&&
Logical AND
Both conditions must be true
||
Logical OR
At least one must be true
!
Logical NOT
Reverse the condition
Quick if Statement Review
What does == check for?
Equality - are two values the same?
What's the difference between = and ==?
= assigns, == compares
When does code inside { } run?
Only when the condition is TRUE
What's > check?
Greater than comparison
Decision-Making Projects Showcase
Let's see your intelligent programs!
🎓 Grade Classifier: Show your grade categorization with messages!
🌡️ Weather Advisory: Demonstrate temperature-based clothing suggestions!
🚗 Speed Monitor: Display your speeding warning system!
🎂 Birthday Party Planner: Show age-based party suggestions!
🏆 Sports Performance: Demonstrate athletic performance classification!
🔐 Password System: Show multi-level access control!
Amazing decision-making logic!
The Problem with Multiple if Statements
int score = 85;
if (score >= 90) {
printf("Grade: A\n");
}
if (score >= 80) {
printf("Grade: B\n");
}
if (score >= 70) {
printf("Grade: C\n");
}
Problem: Computer checks ALL conditions even after finding a match!
Output: "Grade: B" and "Grade: C" both print! 😱
Meet if-else - The Smart Solution!
🔀
The if-else Power:
if (condition) {
} else {
}
✨ Key Benefit: Only ONE block of code runs - no redundancy!
How if-else Works - Visual Flow
Check Condition
⬇️
✅ TRUE
Execute if block
❌ FALSE
Execute else block
⬇️
Continue Program
Unlike multiple if statements, if-else guarantees exactly ONE path is taken!
Your First if-else Program
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You can vote! 🗳️\n");
} else {
printf("You cannot vote yet. 🚫\n");
printf("Wait %d more years!\n", 18 - age);
}
return 0;
}
Test Results:
- Age 20 → "You can vote!" ✅
- Age 15 → "You cannot vote yet. Wait 3 more years!" ❌
Multiple ifs VS if-else Comparison
❌ Multiple ifs (Inefficient)
int num = 5;
if (num > 0) {
printf("Positive\n");
}
if (num < 0) {
printf("Negative\n");
}
if (num == 0) {
printf("Zero\n");
}
Checks all 3 conditions!
VS
✅ if-else (Efficient)
int num = 5;
if (num > 0) {
printf("Positive\n");
} else if (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
Stops after first match!
The Power of else-if Chains
#include <stdio.h>
int main() {
int score;
printf("Enter your score (0-100): ");
scanf("%d", &score);
if (score >= 90) {
printf("Grade: A - Excellent! 🌟\n");
} else if (score >= 80) {
printf("Grade: B - Good job! 👍\n");
} else if (score >= 70) {
printf("Grade: C - Keep working! 📚\n");
} else if (score >= 60) {
printf("Grade: D - Need improvement! 💪\n");
} else {
printf("Grade: F - Let's study together! 📖\n");
}
return 0;
}
Meet the Logical Operators!
🧠
&&
AND Operator
Both conditions must be TRUE
true && true = true
true && false = false
||
OR Operator
At least one must be TRUE
true || false = true
false || false = false
!
NOT Operator
Reverses the condition
!true = false
!false = true
The && (AND) Operator - Both Must Be True
#include <stdio.h>
int main() {
int age, hasLicense;
printf("Enter your age: ");
scanf("%d", &age);
printf("Do you have a license? (1=yes, 0=no): ");
scanf("%d", &hasLicense);
if (age >= 16 && hasLicense == 1) {
printf("You can drive! 🚗\n");
} else {
printf("You cannot drive yet. 🚫\n");
}
return 0;
}
The || (OR) Operator - At Least One Must Be True
#include <stdio.h>
int main() {
char day;
printf("Enter day (S=Saturday, U=Sunday): ");
scanf(" %c", &day);
if (day == 'S' || day == 'U') {
printf("It's the weekend! 🎉\n");
printf("Time to relax!\n");
} else {
printf("It's a weekday. 📚\n");
printf("Time for school/work!\n");
}
return 0;
}
The ! (NOT) Operator - Reverse the Logic
#include <stdio.h>
int main() {
int isRaining;
printf("Is it raining? (1=yes, 0=no): ");
scanf("%d", &isRaining);
if (!isRaining) {
printf("Great! Let's go to the park! ☀️\n");
} else {
printf("Stay inside and code! 💻\n");
}
return 0;
}
Without NOT (!)
if (isRaining == 0)
Checks if raining is equal to 0
With NOT (!)
if (!isRaining)
Reverses: if NOT raining
Logical Operators Quiz
If age = 15 and hasPermit = 1, what's the result?
if (age >= 16 && hasPermit == 1) {
printf("Can drive!\n");
}
A) TRUE - Prints "Can drive!"
Both conditions are met
B) FALSE - Nothing prints
Age is not >= 16
D) TRUE - hasPermit is 1
Permit is enough
Nested if Statements - Decisions Within Decisions
#include <stdio.h>
int main() {
int age, hasTicket;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 13) {
printf("Do you have a ticket? (1=yes, 0=no): ");
scanf("%d", &hasTicket);
if (hasTicket == 1) {
printf("Enjoy the movie! 🎬\n");
} else {
printf("Please buy a ticket first! 🎫\n");
}
} else {
printf("Sorry, you're too young for this movie. 🚫\n");
}
return 0;
}
Combining Everything - Complex Conditions
#include <stdio.h>
int main() {
int score, attendance;
printf("Enter your score: ");
scanf("%d", &score);
printf("Enter your attendance (0-100): ");
scanf("%d", &attendance);
if (score >= 90 && attendance >= 95) {
printf("🏆 HONOR ROLL! Congratulations!\n");
} else if (score >= 90 || attendance >= 95) {
printf("👍 Great job! Keep it up!\n");
} else {
printf("💪 Keep working hard!\n");
}
return 0;
}
Your Turn: Complete Grade Calculator with GPA
Build a Professional Grade System
1. Get student's score (0-100)
2. Use else-if chain for letter grades
3. Display letter grade and GPA equivalent
4. Add motivational messages
5. Handle invalid scores (< 0 or > 100)
GPA Scale:
- A (90-100) = 4.0 GPA
- B (80-89) = 3.0 GPA
- C (70-79) = 2.0 GPA
- D (60-69) = 1.0 GPA
- F (below 60) = 0.0 GPA
Common Logical Operator Mistakes
Wrong: Chaining Comparisons
if (10 < age < 20) // WRONG!
Correct:
if (age > 10 && age < 20)
Wrong: Using = instead of ==
if (age = 18) // ASSIGNS!
Correct:
if (age == 18)
Wrong: OR when you need AND
if (age >= 16 || hasLicense)
Allows driving without license OR at wrong age!
Correct:
if (age >= 16 && hasLicense)
Pro Tips
- Use parentheses for clarity
- Test with multiple inputs
- Read conditions aloud
- Draw truth tables if confused
Advanced Decision-Making Mastery
if-else Expert
else-if Chain Master
AND (&&) Operator Pro
OR (||) Logic Ninja
NOT (!) Reverser
Nested if Builder
Your programs now make intelligent, efficient decisions
You've mastered advanced conditional logic!
Your Advanced Decision-Making Mission
This Week's Challenges:
- Complete Grade Calculator with GPA - Include all features discussed
- Movie Ticket Pricing System - Age-based pricing with matinee discount
- Login System - Username AND password validation
- Season Identifier - Based on month number (1-12)
- Temperature Advisory - Complex weather recommendations
- Student Eligibility Checker - Multiple criteria for scholarship
Next Week: Session 9
Switch Statements - Multiple Choice Decision Making
We'll learn an elegant way to handle menu systems and multiple options!
Think About This Week:
What menu systems would you like to create for your programs?
Excellent work! You're now an advanced decision-making programmer!