How to write switch statement in C#

Poster
Posted by Poster under C# category on | Views : 3634
The switch statement is a conditional statement in C# that is used to test a value of a particular variable and decide the action. In the below code, I am checking the value of "option" variable and deciding the action (value of education variable), if desired value is not found for option, then default value will be set for education variable in the default: code block.


int option = int.Parse(Request["n"].ToString()); ;
string education = string.Empty;

switch (option)
{
case 1:
education = "Matric";
break;
case 2:
education = "I.Sc.";
break;
case 3:
education = "B.Sc";
break;
case 4:
education = "M.Sc";
break;
default:
education = "Reselect option";
break;

}
Response.Write(education);


Thanks

Comments or Responses

Login to post response