In This Article, You Will Learn about Java Switch Case.
Java Switch Case – Before moving ahead, let’s know a bit about Java for Loop.
Table of Contents
Definition Switch Case
Java Switch case is used to choose one option among a number of options for a given variable.
Syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code to be executed either if all cases are not matched;
// or condition matched but no "break" keyword found
}
Explanation
- Switch expression evaluates only once.
- The value of the expression is compared with the value of each case.
- break and default are the optional keywords.
Important Points
- Switch expression should have either one or N number of case blocks of code.
- case value must be the type of Switch expression.
- case value cannot have a String type value.
- Each case value must be distinct, if the same is found, an error occurs.
- In each case break keyword is optional, if the condition matches and found break keyword then the expression stops execution else it executes another case statements too.
- default keyword is optional. It is executed at the last after the execution of the expression statement.
Example: Using the Switch case with the break keyword.
import java.util.Scanner;
public class Myjava {
public static void main(String[] args) {
Scanner foo = new Scanner(System.in); //takes input from user
System.out.println("Enter the current temperature");
int temperature = foo.nextInt(); //converts input in int
foo.close();
switch (temperature) {
case 26:
System.out.println("Today is a cool day.");
break;
case 35:
System.out.println("Today is a little bit hot day.");
break;
case 55:
System.out.println("Today is extremely hot day.");
break;
default:
System.out.println("You entered the wrong input");
}
System.out.println("Have a great day ahead!");
}
}
Explanation: The Switch expression evaluates once and prints the statement of the matched condition.
For example, if the user entered the case value 35, then the “Today is a little bit hot day.” statement will be printed out, and execution will be stopped because of the break keyword.
The default statement will be printed out when the user’s input is not matched with case values.
And finally, when the interpreter comes out of the Switch case, it will print the “Have a great day ahead!” statement. This statement is out of the switch case; therefore, whether a condition matches inside the Switch case or not, this statement will always be printed out.
Example: Using the Switch case without the break keyword.
switch (temperature) {
case 26:
System.out.println("Today is a cool day.");
// break;
case 35:
System.out.println("Today is a little bit hot day.");
// break;
case 55:
System.out.println("Today is extremely hot day.");
// break;
default:
System.out.println("You entered the wrong input");
}
System.out.println("Have a great day ahead!");
}
}
Explanation:
For example, if the user entered the case value 35, then the “Today is a little bit hot day.” statement will be printed out and execution will be forwarded to the next case statement because of the no break keyword, and it will print all given statements until it reaches to the last statement or finds a break keyword in between statements.
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: