Skip to content
dreamcode
dreamcode
Map
Switch statements
Lesson 4 of 21
+15 XP on finish
C# FOUNDATIONSChapter 1 · C# Foundations

Choice with switch

A switch statement evaluates an expression and matches it against one of several case blocks. It is cleaner than a long chain of else-if statements when checking a single value.

Worked example
string mood = "dreamy";
switch (mood) {
    case "neon":
        Console.WriteLine("Bright night");
        break;
    case "dreamy":
        Console.WriteLine("Soft stars");
        break;
    default:
        Console.WriteLine("Neutral sky");
        break;
}

How it reads

  • switch (mood) evaluates the variable mood
  • case "neon": defines a block that runs if mood equals "neon"
  • break; exits the switch statement, avoiding fallthrough
Cloud tip: C# requires a control flow jump (like break, return, or throw) at the end of each non-empty case block. Case fallthrough is not allowed.

Check your understanding

0 / 2

Answer all 2 to complete this lesson and earn 15 XP.

  1. 1. What keyword handles unmatched values in a switch statement?
  2. 2. What is required at the end of each non-empty case block in C#?
Answer every question to unlock the next lesson.