BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Program Flow Control: Conditional Processing

The thirty-second part of the C# Fundamentals tutorial concludes the examination of the program flow control commands available within C#. This article considers the conditional processing commands that allow code to be executed when tests are met.

The Switch Statement

The second conditional processing structure available in C# is the switch statement. It is similar to the if-else-if ladder as it provides multiple branching. In the case of the switch method, the value of a variable or expression it tested against a series of discrete values, or cases, until a match is found. The code within the matched case is then executed. This allows you to choose from multiple actions according to a value.

The switch statement is generally easier to read and more efficient than the if-else-if ladder. However, because it only permits testing of a single expression against a list of discrete values, it is not appropriate for all situations. The syntax for the switch command is:

switch (expression)
{
    case value1:
        commands1;
        break;
    case value2:
        commands2;
        break;
    case value3:
        commands3;
        break;
.
.
.
    default:
        commands;
        break;
}

The first line is the switch keyword and the expression to be tested. The expression must generate an integer type, char or string. All other types cannot be used.

Each case element defines a value to compare against the original expression. If the value is an exact match the commands within the case are executed. Note that in the switch statement, each case may have several commands without the need for a code block. Instead, to indicate that the case is complete, a break command is the last statement. A default case can be added at the end of the switch statement. If used, this is executed when no match is found.

The following example shows the switch command used to display a message according to a status code:

string statusCode = "OK";

switch (statusCode)
{
    case "OK":
        Console.WriteLine("Completed.");            // Outputs "Completed"
        break;
    case "Warn":
        Console.WriteLine("Completed with warnings.");
        break;
    case "Err":
        Console.WriteLine("Completed with errors.");
        break;
    case "Fail":
        Console.WriteLine("Failed to complete.");
        break;
    default:
        Console.WriteLine("Unknown status code");
        break;
}

In some situations, you may wish to execute the same code for several cases. This can be achieved with a series of empty cases. When a selected case contains no code, the contents of the next non-empty case statement are used instead. The previous example could be extended to select from multiple status codes with the same results, as follows:

string statusCode = "OK";

switch (statusCode)
{
    case "OK":
    case "NoError":
        Console.WriteLine("Completed.");            // Outputs "Completed"
        break;
    case "Warn":
    case "Warning":
    case "Warnings":
        Console.WriteLine("Completed with warnings.");
        break;
    case "Err":
    case "Error":
    case "Errors":
        Console.WriteLine("Completed with errors.");
        break;
    case "Fail":
    case "Failed":
        Console.WriteLine("Failed to complete.");
        break;
    default:
        Console.WriteLine("Unknown status code");
        break;
}
11 March 2007