Friends can you give me some JAVA program using the switch case and loop
for class 9.
Answers
In Java, the if..else..if ladder executes a block of code among many blocks. The switch statement can a substitute for long if..else..if ladders which generally makes your code more readable.
The syntax of switch statement is:
switch (variable/expression) {
case value1:
// statements
break;
case value2:
// statements
break;
.. .. ...
.. .. ...
default:
// statements
}
The switch statement evaluates it's expression (mostly variable) and compares with values(can be expression) of each case label.
The switch statement executes all statements of the matching case label.
Suppose, the variable/expression is equal to value2. In this case, all statements of that matching case is executed.
Notice, the use of break statement. This statement terminates the execution of switch statement. The break statements are important because if they are not used, all statements after the matching case label are executed in sequence until the end of switch statement.