In java, the switch is a control statement just like if else-if ladder. The switch statement will execute only one statement from several conditions. You can use String, int, char, and boolean in a switch statement.
Following is the diagram of the switch statement in java
Syntax of the switch statement in java
switch(expression)
{
case 1:
// statement
break;
case 2:
// statement
break;
case 3:
// statement
break;
default:
// statement
break;
}
Keywords in java switch
Keywords | Description |
---|---|
switch | switch keyword is used to express the switch statement in java |
case | case keyword is used to check the conditions |
break | break keyword is used to break the switch statement |
default | default keyword is used to execute the default statement. If not a single case condition is the match |
Example of the switch statement in java
package LearnJavaSkills.in;
public class SwitchStatement
{
public static void main(String[] args)
{
int number = 4;
switch (number)
{
case 1:
System.out.println("number is 1");
break;
case 2:
System.out.println("number is 2");
break;
case 3:
System.out.println("number is 3");
break;
case 4:
System.out.println("number is 4");
break;
default:
System.out.println("default");
break;
}
}
}
Simplification of the above code
In the above java program, we have generated one simple java switch example
In this example, we have declared one int variable which is number. We are examining four various conditions on the number
To check the condition in the switch statement, we have one keyword provided by java which is the case. The Syntax for using the case in java is the case (condition)
In the first case condition, we are looking over if the number value is equal to 1. If the number value is equal to 1 then, the program will execute the case 1 statement and after that, it will break
If the first case does not match then, just like case 1, all four cases will look over one-by-one until the case are matched
If not a single case-matched then, the default will execute his statement, however in our program, case 4 is the perfect match, that's why case 4 statement is executed
Using string in a Java switch statement
package LearnJavaSkills.in;
import java.time.DayOfWeek;
import java.time.LocalDate;
public class UsingStringInSwitch
{
public static void main(String[] args)
{
DayOfWeek dayOfWeek = DayOfWeek.from(LocalDate.now());
String todaysDay = dayOfWeek.toString();
System.out.print( "Today is " + dayOfWeek.toString() + " and your task is to ");
switch(todaysDay)
{
case "MONDAY" :
System.out.println("Learn loops.");
break;
case "TUESDAY" :
System.out.println("Learn condition statements.");
break;
case "WEDNESDAY" :
System.out.println("Learn inheritance.");
break;
case "THURSDAY" :
System.out.println("Learn abstraction.");
break;
case "FRIDAY" :
System.out.println("Learn encapsulation.");
break;
case "SATURDAY" :
System.out.println("Learn polymorphism.");
break;
case "SUNDAY" :
System.out.println("Enjoy your day");
break;
default :
System.out.println("Some thing went wrong");
break;
}
}
}
Simplification of the above code
In the above program, we have simply created a to-do list using java
We have used one enum, which is DayOfWeek. DayOfWeek is of java.time package. DayOfWeek is used to get the current day of the week
The code for retrieving current day in java is
DayOfWeek dayOfWeek = DayOfWeek.from(LocalDate.now());
String todaysDay = dayOfWeek.toString();(code-box)
In this code, we have used DayOfWeek enum as we discuss early
And we have called a static method from of DayOfWeek enum to fetch today's day
from expecting one parameter TemporalAccessor which is an interface in java
So, here we had used now the static method of LocalDate which return the LocalDate object
If you are using IDE then, you can right-click on LocalDate to look over the code. Here you will notice that the LocalDate class implement the TemporalAdjuster interface
And finally, to convert enum value in the string, we used
dayOfWeek.toString();(code-box)
After retrieving the current day, we have assigned the current day in switch expression to get the today task
Now switch statement will validate the current day and will print the task as per our program
The nested Switch statement in Java
Following is the syntax of the nested switch in java also known as a switch inside the switch
switch(expression)
{
case 1 :
// statement
break;
case 2 :
// statement
break;
case 3 :
switch(expression)
{
case 1:
// statements
break;
case 2:
// statements
break;
case 3:
// statements
break;
default:
// statements
break;
}
case 4:
// statements
break;
default :
// statements
break;
}
NOTE: you can use as many switch inside but for better practice don't use a nested switch unless and until you don't have any choice(alert-passed)
If you want to dir your hand on the nested switch then, create one example in nested switch
Conclusion
In this tutorial, we have learned what is switch statement in java and how to use them in java. We also learn how to create a simple to-do list in a switch statement.
Thanks for reading and keep learning