Lesson 4.3

Switch Case Statements C# - With Programming Example


Objective

What is Switch Case Statement in C#?
Syntax of Switch Case Statements
Programming Example

What is Switch Case in C#?

Switch is a control statements that holds one or more case and execute them based on condition matched. It makes program simpler when you need to use multiple IF condition.

Example

If Else

Syntax

switch(expression) {
   case constant-expression  :
      statement(s);
      break; /* optional */
   case constant-expression  :
      statement(s);
      break; /* optional */
  
   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}

Notes

  1. A switch may have many cases.
  2. If no any condition matches then default clause executed.
  3. Use break clause to exit from switch case.

Programming Example

In this program, we are writing a program for simple calculator that takes input at runtime and shows selected output.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace switch_case
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2;
            int option;
            int result;

            Console.WriteLine("Welcome to Calculator: Enter your Option (1-5)");
            Console.WriteLine("1. Add");
            Console.WriteLine("2. Sub");
            Console.WriteLine("3. Multiply");
            Console.WriteLine("4. Divide");

            Console.Write("Enter your option: ");
            option = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter 1st No: ");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter 2nd No: ");
            num2 = Convert.ToInt32(Console.ReadLine());

            switch (option)
            {
                case 1:                    
                    result = num1 + num2;
                    Console.WriteLine("Add : " + result);
                    break;
                case 2:
                    result = num1 - num2;
                    Console.WriteLine("Sub : " + result);
                    break;
                case 3:
                    result = num1 * num2;
                    Console.WriteLine("Multiply : " + result);
                    break;
                case 4:
                    result = num1 / num2;
                    Console.WriteLine("Divide : " + result);
                    break;
                default:
                    Console.WriteLine("Invalid Option Entered");
                    break;
            }
            Console.ReadKey();
        }
    }
}

	

Output

Welcome to Calculator: Enter your Option (1-5)
1. Add
2. Sub
3. Multiply
4. Divide

Enter your option: 1

Enter 1st No: 34
Enter 2nd No: 67

Add : 101

__

Summary

In this program you have learned what switch case is and how to use it in program. In the next chapter you will get some programming exercise of conditional statements.

BackNext