9.5. The switch Statement

The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body. The switch statement takes this form:

switch (expression)
{
  case constant-expression:
    statement
    jump-statement
  default:
    statement
    jump-statement]
}

where

  • expression is an integral or string type expression.

  • statement is the embedded statement(s) to be executed if control is transferred to the case or the default.

  • jump-statement transfers control out of the case body.

  • constant-expression is the value that causes control to be transferred to a specific case.

Control is transferred to the case statement whose constant-expression matches expression. The switch statement can include any number of case instances, but no two case constants within the same switch statement can have the same value. Execution of the statement body begins at the selected statement and proceeds until the jump-statement transfers control out of the case body.

Notice that the jump-statement is required after each block, including the last block. C# does not support an explicit fall through from one case label to another. The jump-statement can be a goto, a switch-case, or a goto default.

If expression does not match any constant-expression, control is transferred to the statements that follow the optional default label. If there is no default label, control is transferred outside the switch.

Listing 9.6 shows how control can be transferred using the goto case jump statement. Jump statements are detailed in Section 9.7.

Listing 9.6. C# switch Statement
using System;

public class Test {

  public static void Main(string[] args) {

    Console.WriteLine("Coffee sizes: 1=Small 2=Medium
    3=Large");
    Console.Write("Please enter your selection: ");
    string s = Console.ReadLine();
    int n = int.Parse(s);
    int cost = 0;
    switch(n) {
      case 0:
      case 1:
        cost += 25;
        break;
      case 2:
        cost += 25;
        goto case 1;
      case 3:
        cost += 50;
        goto case 1;
      default:
        Console.WriteLine("Invalid selection. Please select 1,
              2, or 3.");
        break;
    }
    if (cost != 0)
      Console.WriteLine("Please insert {0} cents.", cost);
    Console.WriteLine("Thank you for your business.");
  }

}

Listing 9.7 shows how C# allows strings to be used in switch statements. This should come as a relief to Java programmers who have been bickering about the lack of string support in the switch statement. Note that this comparison is case-sensitive.

Listing 9.7. Using Strings in the C# switch Statement
using System;

public class Test {

  public static void Main(string[] args) {

    string s = args[0];
      switch(s) {
        case "Hi":
        case "Hello":
          Console.WriteLine("Greeting");
          goto case "Ouch";
        case "Ouch":
          Console.WriteLine("Pain ");
          break;
        case "Mmmm":
          Console.WriteLine("Gourmet Food ");
          break;
        default:
          Console.WriteLine("Invalid selection. Please select
                Hi, Hello, Mmmm or Ouch");
          break;
    }
  }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.142.115