๐Ÿ  Session 8 Homework

Advanced Conditionals

๐Ÿ“š What You'll Practice

This homework focuses on mastering advanced conditional statements including if-else, else-if chains, logical operators, and nested conditions.

Goal: Become an expert in complex decision-making logic!

๐Ÿ“‹ Instructions

๐Ÿ“Š Program 1: Grade Calculator with if-else

Use if-else chains for efficient grading!

What it does:

Uses else-if chains to efficiently determine letter grades based on score ranges, stopping at the first match.

#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; }

๐Ÿงช Test Cases

  • Score 95 โ†’ Grade A
  • Score 85 โ†’ Grade B
  • Score 75 โ†’ Grade C
  • Score 65 โ†’ Grade D
  • Score 45 โ†’ Grade F

๐Ÿ”— Program 2: Logical Operators Practice

Master AND, OR, and NOT operators!

What it does:

Demonstrates the use of logical operators (&&, ||, !) in complex conditions for decision making.

#include <stdio.h> int main() { int age, hasLicense, hasInsurance; char day; printf("Enter your age: "); scanf("%d", &age); printf("Do you have a license? (1=yes, 0=no): "); scanf("%d", &hasLicense); printf("Do you have insurance? (1=yes, 0=no): "); scanf("%d", &hasInsurance); printf("Enter day (W=weekday, S=weekend): "); scanf(" %c", &day); printf("\n=== LOGICAL OPERATOR RESULTS ===\n"); // AND operator (&&) if (age >= 18 && hasLicense == 1) { printf("โœ… You can drive! (Age >= 18 AND has license)\n"); } else { printf("โŒ You cannot drive yet.\n"); } // OR operator (||) if (day == 'S' || day == 'W') { printf("โœ… Valid day entered! (Weekend OR Weekday)\n"); } else { printf("โŒ Invalid day!\n"); } // NOT operator (!) if (!(age < 16)) { printf("โœ… You are 16 or older! (NOT under 16)\n"); } else { printf("โŒ You are under 16.\n"); } // Complex condition with multiple operators if ((age >= 18 && hasLicense == 1) || (age >= 21 && hasInsurance == 1)) { printf("โœ… You can rent a car! (Complex condition)\n"); } else { printf("โŒ You cannot rent a car.\n"); } return 0; }

๐Ÿงช Test Cases

  • Age 20, License 1, Insurance 0, Day W โ†’ Can drive, Valid day, 16+, Can rent
  • Age 17, License 0, Insurance 1, Day S โ†’ Cannot drive, Valid day, 16+, Cannot rent
  • Age 25, License 1, Insurance 1, Day W โ†’ Can drive, Valid day, 16+, Can rent

๐Ÿ  Program 3: Nested If Statements

Create complex decision trees!

What it does:

Demonstrates nested if statements for complex decision-making scenarios with multiple levels of conditions.

#include <stdio.h> int main() { int age, hasTicket, hasMoney; char movieType; printf("=== MOVIE THEATER ACCESS ===\n"); printf("Enter your age: "); scanf("%d", &age); printf("Do you have a ticket? (1=yes, 0=no): "); scanf("%d", &hasTicket); printf("Do you have money? (1=yes, 0=no): "); scanf("%d", &hasMoney); printf("Movie type (G=General, P=PG-13, R=Rated R): "); scanf(" %c", &movieType); printf("\n=== ACCESS DECISION ===\n"); if (age >= 13) { printf("โœ… Age check passed!\n"); if (hasTicket == 1) { printf("โœ… You have a ticket!\n"); if (movieType == 'R') { if (age >= 17) { printf("โœ… You can watch the R-rated movie!\n"); } else { printf("โŒ You're too young for R-rated movies!\n"); } } else if (movieType == 'P') { printf("โœ… You can watch the PG-13 movie!\n"); } else if (movieType == 'G') { printf("โœ… You can watch the General movie!\n"); } else { printf("โŒ Invalid movie type!\n"); } } else { printf("โŒ You need a ticket first!\n"); if (hasMoney == 1) { printf("๐Ÿ’ก You can buy a ticket at the counter!\n"); } else { printf("๐Ÿ’ธ You need money to buy a ticket!\n"); } } } else { printf("โŒ You're too young for this theater!\n"); } return 0; }

๐Ÿงช Test Cases

  • Age 20, Ticket 1, Money 1, Type R โ†’ Can watch R-rated movie
  • Age 15, Ticket 1, Money 1, Type P โ†’ Can watch PG-13 movie
  • Age 18, Ticket 0, Money 1, Type G โ†’ Can buy ticket
  • Age 10, Ticket 1, Money 1, Type G โ†’ Too young for theater

๐Ÿงฉ Program 4: Complex Condition Builder

Build and test complex logical expressions!

What it does:

Allows users to test various combinations of logical conditions and see how they evaluate to true or false.

#include <stdio.h> int main() { int a, b, c; int result1, result2, result3, result4; printf("=== COMPLEX CONDITION BUILDER ===\n"); printf("Enter three numbers: "); scanf("%d %d %d", &a, &b, &c); printf("\nValues: a=%d, b=%d, c=%d\n", a, b, c); // Test various complex conditions result1 = (a > b) && (b > c); result2 = (a > b) || (b > c); result3 = !(a > b); result4 = (a > b) && (b > c) || (a > c); printf("\n=== CONDITION RESULTS ===\n"); printf("(a > b) && (b > c): %s\n", result1 ? "TRUE" : "FALSE"); printf("(a > b) || (b > c): %s\n", result2 ? "TRUE" : "FALSE"); printf("!(a > b): %s\n", result3 ? "TRUE" : "FALSE"); printf("(a > b) && (b > c) || (a > c): %s\n", result4 ? "TRUE" : "FALSE"); // Practical example: Student eligibility int score = a; int attendance = b; int projects = c; printf("\n=== STUDENT ELIGIBILITY EXAMPLE ===\n"); printf("Score: %d, Attendance: %d%%, Projects: %d\n", score, attendance, projects); if ((score >= 80 && attendance >= 90) || (score >= 90 && projects >= 3)) { printf("โœ… Student is eligible for honors!\n"); } else if (score >= 70 && attendance >= 80) { printf("โœ… Student passes the course.\n"); } else { printf("โŒ Student needs improvement.\n"); } return 0; }

๐Ÿงช Test Cases

  • Input: 10, 5, 3 โ†’ Various logical evaluations
  • Input: 85, 95, 4 โ†’ Student eligible for honors
  • Input: 75, 85, 2 โ†’ Student passes course
  • Input: 60, 70, 1 โ†’ Student needs improvement

๐Ÿ“‹ Program 5: Menu-Driven Decision System

Create an interactive decision-making system!

What it does:

Creates a comprehensive menu system that uses all types of conditional statements for different decision-making scenarios.

#include <stdio.h> int main() { int choice, age, score, temperature; char grade, weather; printf("=== INTERACTIVE DECISION SYSTEM ===\n"); printf("1. Age-based decisions\n"); printf("2. Grade evaluation\n"); printf("3. Weather recommendations\n"); printf("4. Score analysis\n"); printf("Enter your choice (1-4): "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter your age: "); scanf("%d", &age); if (age < 13) { printf("You're a child! Enjoy playing! ๐Ÿง’\n"); } else if (age >= 13 && age < 20) { printf("You're a teenager! Focus on studies! ๐ŸŽ“\n"); } else if (age >= 20 && age < 65) { printf("You're an adult! Work hard! ๐Ÿ’ผ\n"); } else { printf("You're a senior! Enjoy retirement! ๐Ÿ‘ด\n"); } break; case 2: printf("Enter your grade (A/B/C/D/F): "); scanf(" %c", &grade); if (grade == 'A' || grade == 'a') { printf("Excellent work! Keep it up! ๐ŸŒŸ\n"); } else if (grade == 'B' || grade == 'b') { printf("Good job! You're doing well! ๐Ÿ‘\n"); } else if (grade == 'C' || grade == 'c') { printf("Average work. Try to improve! ๐Ÿ“š\n"); } else if (grade == 'D' || grade == 'd') { printf("Below average. Study more! ๐Ÿ’ช\n"); } else if (grade == 'F' || grade == 'f') { printf("Failed. Let's work together! ๐Ÿ“–\n"); } else { printf("Invalid grade entered!\n"); } break; case 3: printf("Enter temperature: "); scanf("%d", &temperature); printf("Weather type (S=sunny, R=rainy, C=cloudy): "); scanf(" %c", &weather); if (temperature > 80 && weather == 'S') { printf("Perfect beach weather! ๐Ÿ–๏ธ\n"); } else if (temperature < 50 || weather == 'R') { printf("Stay indoors! โ˜”\n"); } else if (temperature >= 60 && temperature <= 80) { printf("Nice weather for a walk! ๐Ÿšถ\n"); } else { printf("Check the weather before going out! ๐ŸŒค๏ธ\n"); } break; case 4: printf("Enter your test score: "); scanf("%d", &score); if (score >= 90) { printf("Outstanding! You're in the top 10%%! ๐Ÿ†\n"); } else if (score >= 80) { printf("Great job! Above average performance! ๐ŸŽฏ\n"); } else if (score >= 70) { printf("Good work! You're passing! โœ…\n"); } else if (score >= 60) { printf("Barely passing. Study more! ๐Ÿ“–\n"); } else { printf("Failing grade. Let's work together! ๐Ÿค\n"); } break; default: printf("Invalid choice! Please select 1-4.\n"); } return 0; }

๐Ÿงช Test Cases

  • Choice 1, Age 15 โ†’ Teenager message
  • Choice 2, Grade A โ†’ Excellent work message
  • Choice 3, Temp 85, Weather S โ†’ Perfect beach weather
  • Choice 4, Score 95 โ†’ Outstanding message