Latest web development tutorials

C # switch statement

C # judge C # judge

Test case aswitch statement allows a variable equal to the multiple value.Each value is called a case, and the variable will be tested eachswitch case to be checked.

grammar

Switch statement in C # syntax:

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

switch statement must follow these rules:

  • switch statement expressionmust be an integer or enumeration type, or a class type, which class has a single conversion function to convert it to an integer or enumeration type.
  • In a switch you can have any number of case statements. Each case is followed by a value to compare with a colon.
  • case ofconstant-expression must have the same data type and switch variables, and must be a constant.
  • When the variable being tested is equal to the case of constant time, followed by a case statement will be executed, until it encounters abreak statement.
  • When abreak statement is encountered, switch terminates the control flow jumps to the next line after the switch statement.
  • Not every case needs to includebreak.If the case statement is empty, it may not contain abreak, we willcontinueto control the flow of the subsequent case, until it encounters a break up.
  • C # is not allowed to continue from a switching section to the next switch section. If the case statement has processing statements, it must include additionalbreak or jump statement.
  • Aswitch statement can have an optional default case,it appears at the end of the switch. default case can be used in all of the above case is not true when performing a task. Thebreak in the default case statement is not required.
  • C # does not support explicit label from one case to another case throughout the label. If you want support from a C # explicit case label through to another case labels, goto can use a switch-case or goto default.

flow chart

C # switch statement

Examples

using System;

namespace DecisionMaking
{
    
    class Program
    {
        static void Main (string [] args)
        {
            / * Local variable definitions * /
            char grade = 'B';

            switch (grade)
            {
                case 'A':
                    Console.WriteLine ( "very good!");
                    break;
                case 'B':
                case 'C':
                    Console.WriteLine ( "well done");
                    break;
                case 'D':
                    Console.WriteLine ( "You passed");
                    break;
                case 'F':
                    Console.WriteLine ( "best try again");
                    break;
                default:
                    Console.WriteLine ( "invalid results");
                    break;
            }
            Console.WriteLine ( "Your achievement is {0}", grade);
            Console.ReadLine ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Your score is well B

C # judge C # judge