A Java switch selects a case based on one selector value. In a traditional colon-style switch, use break when execution should stop after the matching case.
A simple example
Match one case
Start withValue 2
Do thisMatch case 2
Resulttwo
The matching case supplies the word. A different value can take the default path.Main.java
class Main {
public static void main(String[] args) throws Exception {
int n = 2;
switch (n) {
case 2 -> System.out.println("two");
default -> System.out.println("other");
}
}
}
Output
two
Route a parcel update
A parcel dashboard receives status updates. A queued parcel and a parcel awaiting retry both belong in the pending tray. Mark a sent parcel done and send unfamiliar statuses for review.
Example: #1 - switch Statement
Choose a greeting based on the language string.
Java source code
class ConditionalStatement {
public static void main(String[] args) {
String language = "Java";
switch(language) {
case "C":
System.out.println("Welcome to the world of 'C'");
break;
case "Java":
System.out.println("Welcome to the world of 'Java'");
break;
case "Kotlin":
System.out.println("Welcome to the world of 'Kotlin'");
break;
}
}
}
Output
Welcome to the world of 'Java'
Advertisements
Example: #2 - switch Statement with a default statement
The default label handles values that match no case.
Java source code
class ConditionalStatement {
public static void main(String[] args) {
String language = "Python";
switch(language) {
case "C":
System.out.println("Welcome to the world of 'C'");
break;
case "Java":
System.out.println("Welcome to the world of 'Java'");
break;
case "Kotlin":
System.out.println("Welcome to the world of 'Kotlin'");
break;
default:
System.out.println("None of the language selected");
}
}
}
Output
None of the language selected
Example: #3 - switch Statement using OR codition
Select a rating message from the score.
Java source code
class ConditionalStatement {
public static void main(String[] args) {
int score = 6;
switch(score) {
case 10:
System.out.println("Extraordinary");
break;
case 9:
System.out.println("Very Good");
break;
case 8:
System.out.println("Good");
break;
case 7:
case 6:
case 5:
System.out.println("Average");
break;
case 4:
case 3:
case 2:
case 1:
System.out.println("Poor");
break;
}
}
}
Output
Average
Example: #4 - switch Statement without break statement
In a colon-style switch, execution can fall through to the following statements until a break, return, or other transfer of control.
Java source code
class ConditionalStatement {
public static void main(String[] args) {
String language = "Java";
switch(language) {
case "C":
System.out.println("Welcome to the world of 'C'");
case "Java":
System.out.println("Welcome to the world of 'Java'");
case "Kotlin":
System.out.println("Welcome to the world of 'Kotlin'");
case "Scala":
System.out.println("Welcome to the world of 'Scala'");
break;
case "Python":
System.out.println("Welcome to the world of 'Python'");
break;
}
}
}
Output
Welcome to the world of 'Java'
Welcome to the world of 'Kotlin'
Welcome to the world of 'Scala'
Example: #5 - Nested switch Statement
Use nested switch statements to select the Java greeting for a basic subscription.
Java source code
class ConditionalStatement {
public static void main(String[] args) {
String language = "Java";
String subscription = "basic";
switch(language) {
case "C":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'C' language is $15");
break;
case "advanced":
System.out.println("Subscription for advanced concepts in 'C' language is $30");
break;
default:
System.out.println("None of the subscription selected on 'C' language");
}
System.out.println("Thanks for choosen the 'C' language");
break;
case "Java":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'Java' language is $20");
break;
case "advanced":
System.out.println("Subscription for advanced concepts in 'Java' language is $35");
break;
default:
System.out.println("None of the subscription selected on 'Java' language");
}
System.out.println("Thanks for choosen the 'Java' language");
break;
case "Kotlin":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'Kotlin' language is $20");
break;
case "advanced":
System.out.println("Subscription for advanced concepts in 'Kotlin' language is $40");
break;
default:
System.out.println("None of the subscription selected on 'Kotlin' language");
}
System.out.println("Thanks for choosen the 'Kotlin' language");
break;
default:
System.out.println("None of the language selected");
}
}
}
Output
Subscription for basics of 'Java' language is $20
Thanks for choosen the 'Java' language