Understanding Switch Statement with the help of an example

About Switch Statement

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:
	    //your code here
	    break;//Jump Statement
      default:
	    //your code here
	    break;
}    

For Example

 
switch (condition){
      case "Add":
	    int add = num1 + num2;
	    break;//Jump Statement
      default:
	    int add = 0;
	    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");
            string condition = Console.ReadLine();
          }

Third Step to take Two Numbers as Input

 
          static void Main(string[] args)
	    {
		Console.WriteLine("Enter a first number");
            int num1 = Convert.ToInt16(Console.ReadLine());

            Console.WriteLine("Enter a second number");
            int num2 = Convert.ToInt16(Console.ReadLine());
          }

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");
            int num1 = Convert.ToInt16(Console.ReadLine());

            Console.WriteLine("Enter a second number");
            int num2 = Convert.ToInt16(Console.ReadLine());

            switch (condition){

                case "Add":
                    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.

Author: Gurunatha Dogi

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

Add a Comment