Conditionals (if-else & switch)

🔷 C# Programming Lesson 4 Beginner

Conditionals control the branch paths of execution based on boolean checks. In this lesson, we will look at if-else blocks, ternary operators, and modern C# switch expressions.

1 Modern C# Switch Expressions

C# 8.0 introduced modern **Switch Expressions** which use the lambda/arrow syntax (`=>`). They are clean, prevent fall-through bugs, and return values directly, making them superior to classic switch statements.

2 Conditional Codes

Let's run a program evaluating conditions and checking modern switch expressions:

C# — Conditionals ▶ Run Code
using System;

class Program {
    static void Main() {
        int score = 85;

        // If-else structure
        if (score >= 90) {
            Console.WriteLine("Grade: A");
        } else if (score >= 80) {
            Console.WriteLine("Grade: B");
        } else {
            Console.WriteLine("Grade: F");
        }

        // Modern Switch Expression (C# 8.0+)
        int dayNum = 3;
        string dayName = dayNum switch {
            1 => "Monday",
            2 => "Tuesday",
            3 => "Wednesday",
            _ => "Unknown Day" // '_' acts as the default case
        };
        
        Console.log("Selected Day: " + dayName);
    }
}
3 Code Challenge
Challenge: Write a nested conditional checking if a person is eligible to rent a car. They must be age 21+ and hold a valid credit card. Print an appropriate status message for both success and failure states.