In this example, i’ll show you how to use switch case for a range of number in C#.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
static void Main(string[] args) { Console.Write("Number : "); int number=Convert.ToInt32(Console.ReadLine()); switch (number) { case int n when (n >= 100): Console.WriteLine($"I am 100 or above: {n}"); break; case int n when (n >=50 && n <100): Console.WriteLine($"I am between 99 and 50: {n}"); break; case int n when (n < 50): Console.WriteLine($"I am less than 50: {n}"); break; } Console.ReadLine(); } |
Output: