The python switch statement is a selection statement that allows program control to be transferred to a statement list with a switch label that corresponds to the switch expression's value.
A switch statement is a control statement that runs a set of logic depending on a comparison between a controlling expression and the labels given in the switch block. This statement allows the value of a variable or expression to choose a code block for execution from a list of candidates that reflect the various scenarios that could occur.
When opposed to the if..else..if.. ladder, the usage of a switch statement improves efficiency and readability. A switch statement can be followed by another switch statement, resulting in a nested switch, which can outperform other alternatives.
The keyword "switch" is followed by a controlling switch expression (inside parentheses) and a switch block in a switch statement. There might be zero or many switch sections in a switch block. The term "'case" appears in each switch section, followed by a choice (a constant value ending in ":") and the statement list.
An integral type, enum, string, Boolean, or type that can be transformed to an integral type must be supplied within the parenthesis. There can be numerous "case" labels in each switch section. Every switch section must have an unreachable end; as a result, the switch section must finish with a break statement. Within a switch statement, the constants used in the various "case" labels cannot be reused.
Advantages:
The use of a case or switch statement is considered superior to a similar series of if-else if statements in several languages and programming environments because it is:
- It's a lot easier to troubleshoot (e.g. setting breakpoints on code vs. a call table, if the debugger has no conditional breakpoint capability)
- It is less difficult for a person to read.
- It's easier to grasp, which makes it easier to maintain.
- Fixed depth: a series of "if-else if" lines might result in deep nesting, complicating compilation (especially in automatically generated code)
- It's a lot easier to double-check that all values are handled. If some enum values are not handled, compilers may generate a warning.
Furthermore, because it is commonly done using an indexed branch table, an efficient implementation may run significantly quicker than the alternative. [5] For example, choosing program flow based on the value of a single letter is far more efficient than the alternative, resulting in significantly shorter instruction paths. When used in this manner, a switch statement resembles a perfect hash.
A switch statement is made up of two nodes (entry and exit) and one edge between them for each choice in the control-flow graph. A sequence of "if...else if...else if" statements, on the other hand, has an additional node for every instance save the first and last, as well as a corresponding edge. The resulting control-flow graph for the "if" sequences has considerably more nodes and nearly twice as many edges, none of which offer any relevant information. The simple branches of if statements, on the other hand, are conceptually simpler than the complex branch of a switch statement. If given k examples, each of these options increases the cyclomatic complexity by k1.