Home » Java Tutorial » Java Switch Statement with Examples

Java Switch Statement with Examples

For multiple circumstances, the Java switch statement implements one statement. A variable’s compatibility against different values is checked by the switch statement in java.

The java switch is just the same as the if-else-if hierarchy declaration. This offers a simple way to route execution to various sections of code based on the statement meaning.

Table of Contents

How the Java switch is used?

The java switch declaration works with types of byte, short, int, long, enum, string, and some wrapper types such as Byte, Short, Int, and Long. You have been enabled to access strings in the switch statement java since Java 7.

Java switch works like below mentioned:

  • The switch condition is satisfied once.
  • Expression value is contrasted with the values for each instance.
  • The related piece of code is implemented if there is a contest.
  • The usage of the keywords named default and break are not mandatory and will be explained later.

Unique types of Java switch

Java switch case contains 2 types that are used to validate different variables or values. The types of switch java are mentioned below:

⮚ Simple switch case: Simple switch case used in java has only one loop of the switch statement.

⮚ Nested switch case: Nested switch case used in java can have many loops inside one switch loop.

Casual syntax

The casual syntax of java switch statement is mentioned below:

switch(expression of integer or some variable) {
  case p:
    // code or statement
    break;
  case q:
    // code or statement
    break;
  default:
    // code or statement
}

Eamples:

As above mentioned was the syntax of the java switch declaration; presented below is the respective illustration of it which does the usage of switch case java very efficiently.

int day =F;
switch (days_of_week) {
  case A:
    System.out.println("Monday");
    break;
  case B:
    System.out.println("Tuesday");
    break;
  case C:
    System.out.println("Wednesday");
    break;
  case D:
    System.out.println("Thursday");
    break;
  case E:
    System.out.println("Friday");
    break;
  case F:
    System.out.println("Saturday");
    break;
  case G:
    System.out.println("Sunday");
    break;
}
// Output of the illustration will be: "Saturday" (day F or can say that day 6)

Java switch declaration can also work by not using the keyword named break. The below illustration displays use of it without using the keyword break:

int number_find =68;
switch (number_find) {
  case 45:
    System.out.println("45");
  case 89:
    System.out.println("89");
  case 68:
    System.out.println("68");
  deafult:
    System.out.println("The number you entered is not valid!");
}
// Output of the illustration will be: 68
The number you entered is not valid!

The recently updated version of Java enables the programmers to use string as the case declaration. The respective illustration is displayed below:

{
String find_string =java;
int rank =0;
switch (find_string) {
  case “python”:
    rank=2;
    break;
  case “java”:
    rank=1;
    break;
  default:
    rank=0;
    break;
}
   System.out.println("Language rank is: "+ rank);
}
// Output of the illustration will be: Language rank is 1

Nested java switch

The illustration of nested java switch is as following:

{
char letter = ‘B’;
int year_college = 2;
switch (year_college) {
  case 1:
    System.out.println("DS");
    break;
  case 2:
    switch (letter) {
    	  case “A”:
          	System.out.println("Statistics");
          	break;
      	case “B”:
          	System.out.println("Java");
          	break;
}
}
}
//Output of the illustration will be: Java
Do you know?
1. Java Scanner with Examples
2. Java Stack with Examples
3. ParseInt Java
4. Try Catch JAVA
5. For loop Java with Examples
6. Java vs Javascript
7. Java Iterator with Examples
8. Substring Java
9. Java Switch Statement with Examples

Pin It on Pinterest