C# Basics

C# switch Statement

The C# switch statement is a control statement different from the if statement because it evaluates a single expression against a list of multiple possible cases. Every case is related to the single expression and must end with the break or goto case or an empty block. The break statement passes control out of the switch. You can omit a break statement if two cases lead to the same action.

C# switch statement may result in cleaner code than multiple if statements since switch statements require an expression to be compared only once.

Syntax

A switch statement starts by using the switch keyword followed by a switch expression in parentheses. This expression must evaluate the built-in integral types; the bool, char, and enum types; and the string type. After the switch expression, you include multiple case labels that represent the possible values of the switch expression. When the switch expression finds the matching value specified by a case label, the statements inside that case are executed. C# switch statement can also contain a default label that is executed if none of the values specified by the previous case labels match with the switch expression.

When you code a case label or a default label, you must add a colon “:” at the end. Then, if the label contains one or more statements, you must use a break statement to terminate the execution of a switch statement.

C# Switch Statements

C# Switch statement flowchart
C# switch statement flowchart

Switch Statement with a Default Label

You can have as many cases as you want in whatever sequence possible when using a switch statement. The default case is optional and is executed if no other case applies. It’s also a common practice to code the default label last for easy reading.

In the following example, the value of a number variable is 4. None of the cases matches with the value provided by the switch expression. In this case, the default block is executed which prints out “Unknown number!”

C# switch default

Run Demo

OUTPUT

Unknown number!

C# goto Switch Statement

You can use the goto keyword followed by a case label and a semicolon (;) at the end, which will cause the execution to jump to a new location in the code. The target location is defined by the marker in the code, which is called a label. Check the example below.

C# goto switch statement

Run Demo

OUTPUT

Case: B

Switch Statement That Falls Through the Second Case Label

If one or more case labels are empty, the execution will follow the code of the next case block that contains code. This allows for grouping of multiple case labels with the same implementation.

In the following example, if the number equals 1, the code in case 2 will be executed because the case label 1 and 2 are grouped. If a case block is not empty, a break must be present before the next case label; otherwise, the compiler will generate an error.

C# switch statement that falls through the case label

Run Demo

OUTPUT

Case: One and Two

C# Reference | Microsoft Docs


Add Comment

Click here to post a comment