0Pricing
C# Academy · Lesson

Switch Statements

Simplify complex conditional logic with switch statements.

Switch Statements is a free C# Academy lesson on CoddyKit — lesson 3 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Switch Statements

Welcome to the next lesson! In this lesson, you’ll learn how to use switch statements to simplify complex conditional logic in your C# programs. Let’s get started!

Switch Statements — illustration 1

2

What Are Switch Statements?

Switch statements allow you to test a variable against multiple cases and execute specific code depending on the case. They are a cleaner alternative to using multiple if-else statements.

Key Point: Use switch statements when you need to compare a single variable against multiple values.

3

Syntax of a Switch Statement

The basic syntax of a switch statement is:

switch (variable) {

case value1:

// code;

break;

case value2:

// code;

break;

default: // code; }

Example:

using System;

class Program {
    static void Main() {
        string day = "Monday";

        switch (day) {
            case "Monday":
                Console.WriteLine("Start of the work week!");
                break;
            case "Friday":
                Console.WriteLine("Last workday of the week!");
                break;
            default:
                Console.WriteLine("Midweek day.");
                break;
        }
    }
}

4

The Default Case

The default case is executed if no other cases match. It is similar to the else block in an if-else statement.

Example:

Key Point: Always include a default case to handle unexpected values.

using System;

class Program {
    static void Main() {
        int number = 10;

        switch (number) {
            case 1:
                Console.WriteLine("Number is one.");
                break;
            case 2:
                Console.WriteLine("Number is two.");
                break;
            default:
                Console.WriteLine("Number is neither one nor two.");
                break;
        }
    }
}

5

Switch with Multiple Cases

You can group multiple cases to execute the same block of code. Example:

Tip: Grouping cases avoids code duplication.

using System;

class Program {
    static void Main() {
        char grade = 'B';

        switch (grade) {
            case 'A':
            case 'B':
                Console.WriteLine("Good job!");
                break;
            case 'C':
                Console.WriteLine("You passed.");
                break;
            default:
                Console.WriteLine("Invalid grade.");
                break;
        }
    }
}

6

Using Switch with Integer Ranges

Switch statements in C# do not support ranges directly, but you can combine switch with other logic for range-based conditions. Example:

Key Point: Use when to add conditions to cases.

using System;

class Program {
    static void Main() {
        int age = 25;

        switch (age) {
            case int n when (n >= 0 && n <= 12):
                Console.WriteLine("Child");
                break;
            case int n when (n >= 13 && n <= 19):
                Console.WriteLine("Teenager");
                break;
            case int n when (n >= 20):
                Console.WriteLine("Adult");
                break;
            default:
                Console.WriteLine("Invalid age.");
                break;
        }
    }
}

7

When to Use Switch Statements

Switch statements are most useful when:

  • You need to compare a single variable against multiple values.
  • The variable values are discrete (e.g., numbers, strings, or characters).
  • You want a clean and readable alternative to multiple if-else statements.

Tip: Avoid using switch statements for complex logic with many conditions.

8

Practice Challenge

Write a program that asks the user to enter a number between 1 and 7 and prints the corresponding day of the week. Example:

using System;

class Program {
    static void Main() {
        Console.WriteLine("Enter a number (1-7):");
        int day = int.Parse(Console.ReadLine());

        switch (day) {
            case 1:
                Console.WriteLine("Monday");
                break;
            case 2:
                Console.WriteLine("Tuesday");
                break;
            case 3:
                Console.WriteLine("Wednesday");
                break;
            case 4:
                Console.WriteLine("Thursday");
                break;
            case 5:
                Console.WriteLine("Friday");
                break;
            case 6:
                Console.WriteLine("Saturday");
                break;
            case 7:
                Console.WriteLine("Sunday");
                break;
            default:
                Console.WriteLine("Invalid input. Enter a number between 1 and 7.");
                break;
        }
    }
}

9

10

Great Job!

Congratulations! You’ve learned how to use switch statements to simplify conditional logic in C#. In the next lesson, we’ll explore arrays and learn how to manage collections of data efficiently. Let’s keep coding!

Switch Statements — illustration 10

Frequently asked questions

Is the “Switch Statements” lesson free?

Yes — the full text of “Switch Statements” is free to read here on the web, and the C# Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Switch Statements”?

Simplify complex conditional logic with switch statements. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Switch Statements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Control Flow: If-Else Statements
  2. Loops in C#: For, While, and Do-While
  3. Switch Statements
  4. Introduction to Arrays
  5. Working with Strings
← Back to C# Academy