Difference between revisions of "Case Traversal Policy"
(Created page with "A switch statement is an easy way to execute different things for different values of an operand. It does so by using ''cases'', each with its own collection of statements to...") |
(No difference)
|
Latest revision as of 17:27, 13 April 2017
A switch statement is an easy way to execute different things for different values of an operand. It does so by using cases, each with its own collection of statements to execute if matched. Typically, we only want one case to execute, then expect the switch statement to terminate and execution be handed over to the next statement, if any. This is the default behavior, also known as Safe, where the first and only the first matching case is executed.
The fall-through method allows cases to share statements. The switch statement will find the first matching case and execute its statements, then continues, or falls through with the next case, regardless of it matching or not, and then the next case, and so on, until the last case is executed, or until a break statement is reached. In this scenario, you need to manually add break statements to each case that needs to terminate the switch statement.