Testing Multiple Conditions with switch


If you have multiple clauses to test, it makes sense to add a switch statement instead of many if statements. Internally the compiler treats switch statements as if they were multiple if statements. Code-wise, however, switch statements are normally easier to follow and to maintain than several if statements all in a row.

To write a switch statement:

  1. Type the word switch .

  2. Type an open parenthesis ( .

  3. Type the name of a variable that is either of an integral type ( byte , int , long , char , etc.), or a string type.

  4. Type a close parenthesis ) .

  5. Type an open curly bracket { .

  6. Type the word case followed by a space and a literal value, either a number, a string in quotes, or the name of a constant variable, then Type a colon : .

  7. In the next line type the statements that are to be executed if the variable contains the value in the case.

  8. Type the word break and a semicolon ;.

  9. Add other case statements. Repeat steps 6, 7, and 8 for as many cases as you like.

  10. If you want to execute code for any value outside of the cases you specified type the word default followed by a colon :.

  11. Type the commands to execute in the default case.

  12. Type a close curly bracket } ( Figure 3.20 ).

    Figure 3.20 A switch statement is an easy way to condense a number of if statements. In this case a string variable is being evaluated. Depending on the value of name, the program will jump to a particular case statement and execute the code within.
     public string Description {    get    {      string desc="";  switch(name)   {   case "Notebook":  desc = "A small computer";           break;  case "Desktop":  desc = "A big computer";           break;  case "FlatMon":  desc =           "A sweet looking monitor";           break;  case "FatMon":  desc =           "An old-looking fat monitor";           break;  default:  desc = "unknown item";           break;  }  return desc;    } } 

graphics/tick.gif Tips

  • In the old days of C++ if you omitted the break; statement in one of the case sections, the program would execute the statements in the following case; it would fall through to the next case. With C#, the compiler gives you an error if you forget to put the break statement ( Figure 3.21 ).

    Figure 3.21 Don't forget to put the word break; at the end of each case. Failure to do so will result in great embarrassment.
     public string Description {    get    {      string desc="";      switch(name)      {         case "Notebook":           desc = "A small computer";  //<--error can't omit the break;  case "Desktop":            desc = "A big computer";            break;       }       return desc;    } } 
  • In C# you can have multiple cases execute the same code by putting one case statement after another without code in between ( Figure 3.22 ).

    Figure 3.22 Several cases can do the same code. The trick is to not put any code between them. In the code below, if name is either Notebook or FlatMon the code below FlatMon will be executed. The same is true for Desktop and FatMon.
     public string Description {    get    {      string desc="";  switch(name)   {   case "Notebook":   case "FlatMon":  desc =           "something cool I want";           break;  case "Desktop":   case "FatMon":  desc =           "something old I don't want";           break;  default:  desc = "unknown item";           break;  }  return desc;    } } 
  • You can forward one case block to another by using the goto statement ( Figure 3.23 ).

    Figure 3.23 After executing the code in one case, you can jump to another case using the goto statement followed by the name of the case.
     public string Status {    get    {      string status="";      switch(name)      {         case "Notebook":           status = "A small computer";  if (qty == 0)   goto default;  break;         case "Desktop":           status = "A big computer.";  if (qty == 0)   goto default;  break;  default:  //append to status             status += "(item unavailable.)";             break;      }      return status;    } } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net