Switch:
- Multiple-choice situation in which you need to execute a particular set of statements from a number of choices (that is, more than two), depending on the value of an integer variable or expression.
- The switch statement enables you to select from multiple choices.
- The choices in a switch statement are called cases
- A switch statement allows a variable to be tested for equality against a list of values.
- Each value is called a case, and the variable being switched on is checked for each case.
Syntax of Switch statement:
How does the switch statement work?
The expression is evaluated once and compared with the
values of each case label.
- If there is a match, the corresponding code after the matching label is executed. For example, if the value of the variable is equal to 2, the code after case 2: is executed until the break statement is encountered.
- If there is no match, the code after default: is executed.
0 Comments