C# tutorial- Conditional statements-switch case


C# conditional statements-switch case

-Switch case statement

The switch case statement can be used to test for multiple values of a variable. The break keyword lets the program stop when the desire value is met. The default keyword lets the program execute the statements that follow it when the desire value is not met.

switch(variable)

{

case val1: statement1

        break;

case val2: statement2

        break;

………………………

default: statement

}

 

Example:

int age;
Console.WriteLine("Enter your age:");
age=int.Parse(Console.ReadLine());
switch (age)
{
   case 1: Console.WriteLine("You are 1 year old");
           break;
   case 2: Console.WriteLine("You are 2 years old");
           break;
   case 3: Console.WriteLine("You are 3 years old");
           break;
   default: Console.WriteLine("You are older than 3 years");

           break;

          
}




Comments

CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.