# Understanding Switch Statement with the help of an example

Switch statement is form of control selection statements. Switch statement is a set of statements (Like if Statements) from which any one statement is executed depending on Condition or Boolean expression.

Syntax

```
switch (expression)
{
case expression:
break;//Jump Statement
default:
break;
}
```

For Example

```
switch (condition){
int add = num1 + num2;
break;//Jump Statement
default:
break;
}
```

Expression: An integral or string type expression.

Jump-statement or break: It transfers control out of the case body.

Body of the case statement will execute only if Boolean condition matches to the case expression (If True).

Switch statement can have any number of case instances in it.

One case instance label or expression has different from another case instance label or expression.

## Switch Case using an example

Now let's demonstrate a simple example using switch statement.

We are going to demonstrate simple mathematical calculation using a Console Application.

First Step is to Create Console Application :

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Practical2b
{
class Program
{
static void Main(string[] args)
{
……///code snippet
}
}
}

```

Second Step to take Input value

In this step we will ask input value from an user to execute (Add or Subtract or Multiplication or Division).

```
static void Main(string[] args){
Console.WriteLine("Enter a type like Add , Sub , Mul, Div");
}
```

Third Step to take Two Numbers as Input

```
static void Main(string[] args)
{
Console.WriteLine("Enter a first number");

Console.WriteLine("Enter a second number");
}
```

Fourth Step is to Create Switch Statement.

In this step we will create switch statement with different cases of mathematical like Add, Sub, Multiply and Division and with Boolean condition.

Add - It will add two numbers with display output and finally jump statement (break).

Sub - It will subtract two numbers with display output and finally jump statement (break).

Mul - It will multiply two numbers with display output and finally jump statement (break).

Div - It will divide two numbers with display output and finally jump statement (break).

```
static void Main(string[] args)
{
Console.WriteLine("Enter a first number");

Console.WriteLine("Enter a second number");

switch (condition){

Console.WriteLine("Added number, {0}", num1 + num2);
break;
case "Sub":
Console.WriteLine("Substract number, {0}", num1 - num2);
break;
case "Mul":
Console.WriteLine("Multiply number, {0}", num1 * num2);
break;
case "Div":
Console.WriteLine("Divide number, {0}", num1 / num2);
break;
default:
Console.WriteLine("Display number, {0} , {1} ", num1 , num2);
break;

}
}
```

Fifth Step Display Output

Press Ctrl + F5 to run your console application. So friends this is all about switch statement in c#. If you have any query regarding switch case kindly let me know through your comments. If you like this post kindly share this with your friends. ## Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more #### Understanding the concept of pass by value and pass by reference with an example? #### Sealed Classes in C# with an example and explanation #### Understanding the Relationships - Inheritance, Association, Aggregation and Composition in C# 