C# - Example of switch Statement

C# switch statement example: Here, we are going to learn about the switch case statement program/example.
Submitted by IncludeHelp, on April 09, 2019 [Last updated : March 17, 2023]

The switch Statement

A switch statement allows checking a variable/value with a list of values (cases) and executing the block associated with that case.

Syntax

switch(variable/expression)
{
    case <case_value1>:
        statement(s);
        break;
    case <case_value2>:
        statement(s);
        break;
    default:
        break;
}

Note

  • default case is an optional - but it should be used, because no case value is matched then default statements will be printed.
  • "break" is an optional but if we are testing multiple case values together. Like,
        case <case_value1>:
        case <case_value2>:
            statement(s);
            break;
    

C# code to demonstrate the example of switch statement

Here, we are asking for a day number from the user between the range of 0 to 6, and printing day name (example: 0 for Sunday, 1 for Monday and so on...).

// C# - Example of switch Statement

using System;
using System.IO;
using System.Text;

namespace IncludeHelp {
  class Test {
    // Main Method 
    static void Main(string[] args) {
      int day;

      //input the day number
      Console.Write("Enter day number (0-6): ");
      day = Convert.ToInt32(Console.ReadLine());

      //checking 
      switch (day) {
      case 0:
        Console.WriteLine("Sunday");
        break;
      case 1:
        Console.WriteLine("Monday");
        break;
      case 2:
        Console.WriteLine("Tuesday");
        break;
      case 3:
        Console.WriteLine("Wednesday");
        break;
      case 4:
        Console.WriteLine("Thursday");
        break;
      case 5:
        Console.WriteLine("Friday");
        break;
      case 6:
        Console.WriteLine("Saturday");
        break;

      default:
        Console.WriteLine("Invalid Input");
        break;
      }

      //hit ENTER to exit the program
      Console.ReadLine();
    }
  }
}

Output

First run:
Enter day number (0-6): 3
Wednesday

Second run:
Enter day number (0-6): 9
Invalid Input

C# code to check input character is a VOWEL or CONSOTANT using switch statement

Here, we are asking for a character from the user – and checking 1) input character is an alphabet using if-else statement and if it is an alphabet – we are checking for vowels & consonants.

// C# - Example of switch Statement

using System;
using System.IO;
using System.Text;

namespace IncludeHelp {
  class Test {
    // Main Method 
    static void Main(string[] args) {
      char ch;

      //input a character
      Console.Write("Enter a character: ");
      ch = Console.ReadLine()[0];

      //checking for valid alphabet
      if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
        //checking for vowels
        switch (ch) {
        case 'A':
        case 'a':
        case 'E':
        case 'e':
        case 'I':
        case 'i':
        case 'O':
        case 'o':
        case 'U':
        case 'u':
          Console.WriteLine("Input character {0} is a Vowel", ch);
          break;

        default:
          Console.WriteLine("Input character {0} is a Consonat", ch);
          break;
        }
      } else {
        Console.WriteLine("Input character {0} is not a valid alphabet", ch);
      }

      //hit ENTER to exit the program
      Console.ReadLine();
    }
  }
}

Output

First run:
Enter a character: X
Input character X is a Consonat

Second run:
Enter a character: i
Input character i is a Vowel

Third run:
Enter a character: $
Input character $ is not a valid alphabet

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.