InterviewSolution
| 1. |
Program In java to implement the various techniques of inheritance using switch statement |
|
Answer» switch (expression) { case value1: // code to be executed if // expression is equal to value1 break;
case value2: // code to be executed if // expression is equal to value2 break;
... ...
default: // default statements } 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 case label is executed. For example, if the value of the expression is equal to value2, the code after case value2: is executed. If there is no match, the code after default: is executed. Note: We can do the same functionality using the Java if...else...if LADDER. However, the syntax of the switch statement is cleaner and much easier to read and write. Flowchart of switch Statement Flowchart of the Java switch statement Flow chart of the Java switch statement Example 1: Java switch statement // Java Program to check the size // using the switch...case statement class Main { public static void main(String[] args) { int number = 44; String size; // switch statement to check size switch (number) { case 29: size = "Small"; break; |
|