Cisco CloudCenter: Get the Hybrid IT Advantage

Switch Statement

Definition - What does Switch Statement mean?

A switch statement, in C#, is a selection statement that allows for the transfer of program control to a statement list with a switch label that corresponds to the value of the switch expression.

A switch statement is a control statement that executes a set of logic based on the result of a comparison between a controlling expression and the labels specified in the switch block. This type of statement helps in allowing the value of a variable or expression to select the code block for execution from a list of candidates that represent the different possibilities that can occur.

The use of a switch statement results in improved performance and readability when compared to that of the if..else..if.. ladder. A switch statement can contain another switch statement, thus forming a nested switch, which can yield better performance over other approaches.

Techopedia explains Switch Statement

A switch statement contains the keyword "switch" followed by a controlling switch expression (within parentheses) and a switch block. The switch block can include zero or many switch sections. Each switch section contains the keyword "‘case" followed by a choice (a constant value ending with ":") and the statement list.

After evaluating the expression of the switch statement, control will be transferred to the statement following the "case" label that matches the value of the expression. If there is no matching constant specified in the "case" labels, control is transferred to the statement following the "default" label (if it exists) or to the end of the switch statement.

The expression specified within the parentheses must be an integral type, enum, string, Boolean or type that can be converted to integral type. Each switch section can include multiple "case" labels. The end of every switch section must be unreachable; as a result, the switch section has to end with a jump statement like "break". The constants used in the different "case" labels within a switch statement cannot be repeated.

For example, a switch statement can be used to compare the numerical input value from the user with a list of menu options displayed in an application and then execute the application based on the user's selection.

In contrast to C++, the execution of a switch section in C# is not allowed to “fall through” (continue) to the next switch section.

This definition was written in the context of C#
Share this:

Connect with us

Email Newsletter

Join thousands of others with our weekly newsletter

The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.