By 22/07/2015 19 Comments

C# Switch Statement

  • Switch is also called a flow control statement.
  • We can called it advance of if-else.
  • It checks the equality of the expression inside the switch with its case values constants, if value matches it executes otherwise bypassed by compiler.
  • It supports all data types except floating points and decimal data types.
  • It has cases constants which are also called labels.
  • The data type of a case constant must match with the data type of switch expression.
  • Every case has a break statement or keyword which terminates or exit from switch statement.
  • If we add any statement in any case than it must contains a break statement, if there is no any statement in any case then it is not necessary to put a break statement.
  • There is default keyword in switch statement which works like else statement, if no match will be found than that keyword will be executed, if default is not specified nothing will happen.
  • Switch statement mostly used for creating menus.

Syntax:

switch (expression)
{
case constant 1:
statement sequence
break;
case constant 2:
statement sequence
break;
.
.
.
default:
statement sequence
break;
}

STATEMENT DESCRIPTION
switch (expression) Declare switch statement, where (expression) can be any data type except floating points and decimal types.
{ Starting scope of switch statement.
case constant 1: In this statement (case) is a label or keyword, where (constant1:) is any constant value type except floating points and decimal. NOTE: this data type must be compatible with the data type of (expression, mentioned as above).
statement sequence This can be anything, any logic or just print message which will be performed if any case constant value match with its switch expression.
break; This is a keyword which terminates statement sequence or exit from switch statement.
default: This keyword is an optional in switch statement. The (default:) works same as (else) statement, if no any case constant value matches with its switch expression that the (default:) keyword will be executed.
} Closing scope of switch statement.

Example – 1:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            int userRollNumber = 008;

            // Switch statement declaration.
            switch (userRollNumber)
            { // Start switch scope.
                case 001:
                    Console.WriteLine("Roll number is 001.");
                    break;

                case 002: 
                    Console.WriteLine("Roll number is 002.");
                    break;

                case 003:
                case 008:
                    Console.WriteLine("Roll number is 008.");
                    break;

                case 004:
                    Console.WriteLine("Roll number is 004.");
                    break;

                default: Console.WriteLine("Roll no match found !.");
                    break;
            }  // Switch scope end.      
            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   Roll number is 008.
  */
}

Example – 2:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            int userRollNumber = 0789;

            // Switch statement declaration.
            switch (userRollNumber)
            { // Start switch scope.
                case 001:
                    Console.WriteLine("Roll number is 001.");
                    break;

                case 002: 
                    Console.WriteLine("Roll number is 002.");
                    break;

                case 003:
                case 008:
                    Console.WriteLine("Roll number is 008.");
                    break;

                case 004:
                    Console.WriteLine("Roll number is 004.");
                    break;

                default: Console.WriteLine("No match found !");
                    break;
            }  // Switch scope end.      
            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   No match found !
  */
}


REMEMBER:

  • You cannot declare two cases with the same constant values, such as case 1:, case 1:, if you do that a compile time error will be occurred.

 






Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .