Case Switches

Introduction

Imagine you have a list of values that can each respond to a condition as being true but you want to consider only one of those values. To let you check each one of those values, the C# language provides the switch conditional statement. It takes a value as done when calling a method. It also has a body delimited by curly brackets. In its body, each value is preceded by the case keyword. The formula to create a switch statement is:

switch(expression)
{
    case choice1:
        statement1;
	break;
    case choice2;
        statement2;
	break;
    case choice-n;
        statement-n;
	break;
}

The object or expression that holds the value to examine is passed to the parentheses of switch. The body of the switch contains one or more sections that each starts with the case keyword. Each case section considers one possible value that is followed by a colon. After the case statement, write code that must be applied to only that outcome. The case clause must end with break;.

The switch statement accepts different types of expressions or values.

Switching a String

Probably the easiest type of value to deal with is text because it can hold any value. This means that the expression you want to consider could have disparate combinations of characters. Still in the body of the switch, each case section should deal with a valid value of the possible outcomes.

Switching to a Boolean Value

If you want to consider one of two outcomes of a Boolean expression, pass a Boolean variable or an expression that produces a Boolean value to a switch statement. One case would consider a true value and another case would consider a false value.

Switching an Integer

Probably the most regularly used type of value in a switch expression is a natural number. The object or expression passed to the switch can come from any source as long as it represents a constant integer.

The Default and Case Combinations

Switch to the default Outcome

When considering the possible outcomes of a switch statement, at times there will be possibilities other than those listed and you will be likely to consider them. This special case is handled by the default keyword. The default case would be considered if none of the listed cases matches the supplied answer. The formula of the switch statement that considers the default case is:

switch(expression) {
    case choice1:
        statement1;
    	break;
    case choice2:
        statement2;
    	break;
    case choice-n:
        statement-n;
    	break;
    default:
        default-statement;
    	break;
}

In some languages, the default section doesn't require a break keyword because it is the last. In C#, every case and the default section must have its own exit mechanism, which is taken care of by a break keyword.

Case Combinations

Although each case must consider only one value, you may have a situation where different case values must deal with the same outcome. In this case, you can combine those cases. To do this, type case followed by its value and a colon. On the next line, create another case with its value and colon. You can continue that for each value that fits that group. Then write the common code of those cases and end the section with the required break keyword and its semicolon.

Nesting a Conditional Statement in a Case

Each case of a switch statement has its own body in which you can create a conditional statement. This is referred to as nesting.


Previous Copyright © 2001-2019, FunctionX Next