Java - swtich Statement
The switch statement is a good alternative for the if...elseif...
statement and it provides the parallel alternatives. The switch statment internally performs only the equality comparision (eg: a == b)
not like a if statement expression (i.e. swtich statement can't execute the expressions like a > b).
Example: #1 - switch Statement
Print the greeting message in the console using switch statement based on the value of the input string language.
Java Input Screen
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;
}
}
}
Java Output Screen
Welcome to the world of 'Java'
Example: #2 - switch Statement with a default statement
switch Statement with a default statement extractly similar like a else block in if...else... statement
Java Input Screen
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");
}
}
}
Java Output Screen
None of the language selected
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #3 - switch Statement using OR codition
Print the rating message based on the provided score.
Java Input Screen
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;
}
}
}
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #4 - switch Statement without break statement
switch Statement without a break statement
executes all the consecutive case statements until it reaches the
break statement.
Java Input Screen
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;
}
}
}
Java Output Screen
Welcome to the world of 'Java'
Welcome to the world of 'Kotlin'
Welcome to the world of 'Scala'
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #5 - Nested switch Statement
Print the greeting message for the language Java with a basic subscription using nested switch statement.
Java Input Screen
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");
}
}
}
Java Output Screen
Subscription for basics of 'Java' language is $20
Thanks for choosen the 'Java' language
BBMINFO