switch statement

switch statement executes different branches based on the selected value.

It has the following format:


switch(value){
   case selectedValue:
           statements
   case selecteValue:
           statements
   ...        
   default selectedValue:
           statements        

}

The following code displays the detailed message for a grading schema.


using System;

class Program
{
    static void Main(string[] args)
    {
        string ch = "C";
        switch (ch)
        {
            case "A":
                Console.WriteLine("Excellent");
                break;
            case "B":
                Console.WriteLine("Good");
                break;
            case "C":
                Console.WriteLine("OK");
                break;
            case "D":
                Console.WriteLine("No so good");
                break;
            case "E":
                Console.WriteLine("Failed");
                break;
        }
    }
}

The output:


OK

You can use the string and enum value as the switch control value.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.