Java Switch Statements:
Instead of writing many if..else statements, you can use the switch statement.
Note: The working of the switch-case statement is similar to the Java if...else...if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.
How does the switch-case statement work?
The expression is evaluated once and compared with the values of each case.
If expression matches with value1, the code of case value1 are executed. Similarly, the code of case value2 is executed if expression matches with value2
If there is no match, the code of the default case is executed.
Flow chart:
Syntax:
switch(expression)
{
case value1 :
// Statements
break; // break is optional
case value2 :
// Statements
break; // break is optional
....
....
....
default :
// default Statement
}
Important Rules for Java Switch Statements:
Case values must be constants or literals and of the same type as the switch expression.
Duplicate case values are not allowed.
The break statement is used to exit from the switch block. It is optional but recommended to prevent fall-through.
The default case is optional and executes if no case matches the switch expression. It can appear anywhere within the switch block.
Example program:
public class Month
{
public static void main(String args[])
{
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}
output:
Thursday
Break Keyword:
When Java reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
Example program:
public class Break {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break; // Terminate switch after matching case
case 4:
System.out.println("Thursday");
break;
default:
System.out.println("Invalid day");
}
}
}
output:
wednesday
Default Case in Java switch-case
The switch statement also includes an optional default case. It is executed when the expression doesn't match any of the cases.
For example:
public class Main {
public static void main(String[] args) {
int expression = 9;
switch(expression) {
case 2:
System.out.println("Small Size");
break;
case 3:
System.out.println("Large Size");
break;
// default case
default:
System.out.println("Unknown Size");
}
}
}
output:
Unknown Size
Java Enum in switch statement:(TBD)
An Enum is a unique type of data type in java which is generally a collection (set) of constants. More specifically, a Java Enum type is a unique kind of Java class. An Enum can hold constants, methods, etc.
Example program:
enum Car {
lamborghini,tata,audi,fiat,honda
}
public class Main1{
public static void main(String args[]){
Car c;
c = Car.tata;
switch(c) {
case lamborghini:
System.out.println("You choose lamborghini!");
break;
case tata:
System.out.println("You choose tata!");
break;
case audi:
System.out.println("You choose audi!");
break;
case fiat:
System.out.println("You choose fiat!");
break;
case honda:
System.out.println("You choose honda!");
break;
default:
System.out.println("I don't know your car.");
break;
}
}
}
output:
You choose tata!
Java switch statement with String:(TBD)
Java allows you to use string objects in the expression of switch statement. In order to use string
It must be only string object.
Object game = "Hockey"; // It is not allowed
String game = "Hockey"; // It is OK.
String object is case sensitive.
"Hickey" and "hocker" are not equal.
Example program:
// Java Program to implement String on switch statements in Java
class Main {
public static void main(String[] args) {
// create a string
String language = "Java";
switch(language) {
case "Java":
System.out.println(language + " is famous for enterprise applications.");
break;
case "JavaScript":
System.out.println(language + " is famous for frontend and backend.");
break;
case "Python":
System.out.println(language + " is famous for ML and AI.");
break;
default:
System.out.println(language + " not found on record.");
break;
}
}
}
Ternary operator:
Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators.
Syntax:
variable = Expression1 ? Expression2: Expression3
If operates similarly to that of the if-else statement as in Exression2 is executed if Expression1 is true else Expression3 is executed.
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Flowchart of Ternary Operation:
Example program:
class Ternary {
public static void main(String[] args)
{
// variable declaration
int n1 = 5, n2 = 10, max;
System.out.println("First num: " + n1);
System.out.println("Second num: " + n2);
// Largest among n1 and n2
max = (n1 > n2) ? n1 : n2;
// Print the largest number
System.out.println("Maximum is = " + max);
}
}
output:
First num: 5
Second num: 10
Maximum is = 10
References:
https://www.geeksforgeeks.org/switch-statement-in-java/
Top comments (0)