Back to course

The Switch Statement

C# Zero to Hero: Comprehensive Programming Masterclass

The Switch Statement

Instead of writing many if..else statements, you can use switch for cleaner code when checking a single variable against many values.

Syntax

csharp int day = 4; switch (day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; default: Console.WriteLine("Another day"); break; }

Note: The break keyword is essential to stop the execution of more cases.