switch Statement
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: