← Back to C# Course | Chapter 4: Control Flow | Lesson 2 of 7

switch Statement

In this page:

  1. switch Statement

switch Statement

A switch statement compares one value against several case labels and runs the matching block. Each case needs a break (or return) to prevent falling through to the next one. default handles any value that doesn't match a case.

Warning: Unlike C, C# does not allow implicit fallthrough between non-empty cases — each case must end with break, return, or goto.

Example: switch Statement

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        int day = 3;
        string name;

        switch (day)
        {
            case 1:
                name = "Monday";
                break;
            case 2:
                name = "Tuesday";
                break;
            case 3:
                name = "Wednesday";
                break;
            default:
                name = "Unknown";
                break;
        }

        Console.WriteLine(name);
    }
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.