switch (variable) { case value1: // This case will execute, if variable = value1 break; case value2: // This case will execute, if variable = value2 break; . . . case valueN: // This case will execute, if variable = valueN break; default: // default will execute, if all the above cases are false. break; }
using System;
namespace TEPProject
{
class Program
{
static void Main(string[] args)
{
Start:
Console.Write("Please enter student's roll number: ");
int RollNo = int.Parse(Console.ReadLine());
switch(RollNo)
{
case 1:
Console.WriteLine("Name: StudentA");
Console.WriteLine("Age: 18");
break;
case 2:
Console.WriteLine("Name: StudentB");
Console.WriteLine("Age: 17");
break;
case 3:
Console.WriteLine("Name: StudentC");
Console.WriteLine("Age: 16");
break;
case 4:
Console.WriteLine("Name: StudentD");
Console.WriteLine("Age: 20");
break;
case 5:
Console.WriteLine("Name: StudentE");
Console.WriteLine("Age: 21");
break;
default:
Console.WriteLine("No student found.");
break;
}
Console.WriteLine("\n\n");
goto Start;
}
}
}
So, that was all about How to use switch Statement in C#. If you need any help then ask in comments. Will meet you in next tutorial. Till then take care !!! :)
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w