WeCodeAcademy

C# If...Else and Switch

ExampleC#
using System;

class Program
{
    static void Main()
    {
        int score = 72;
        if (score >= 90)
        {
            Console.WriteLine("A");
        }
        else if (score >= 70)
        {
            Console.WriteLine("B");
        }
        else
        {
            Console.WriteLine("C");
        }

        int day = 3;
        string name = day switch
        {
            1 => "Monday",
            2 => "Tuesday",
            3 => "Wednesday",
            _ => "Another day"
        };
        Console.WriteLine(name);
        Console.WriteLine(score >= 50 ? "pass" : "fail");
    }
}
Output
B
Wednesday
pass
Quick check

What does the _ mean in a switch expression?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.