The switch Statement

Besides the if statement, the other conditional statement in C# is the switch statement. This statement acts much like an extended if statement; it can handle multiple tests in a single statement, checking a test expression against various values and executing code when the test expression matches one of the values you've tested it against. Here's what the switch statement looks like formally in C#:

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

Here are the parts of this statement:

  • expression An integral or string type expression used to test against.

  • statement The embedded statement(s) to be executed if the test expression matches the current case statement's ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.