×

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword.

Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in conditional labeling by the support of the switch statements.

The block of code which contains a label of several cases on which it is runned or executed only when the given value to the switch matches with the provided cases?

To execute the label of the case which is branched with the switch statement the code of the program should pass the arguments in the switch case.There is no limitation for the case block, as it doesn’t have any internal ending point.The switch statement can provide with an multiple case labels.

Each label can hold the various values like integers or strings or statements.The block of code can also contain the break statements which help in terminating the flow of execution from that block of switch statements.

NOTE: The values of Boolean type, float type, double type cannot be hold by the case label.

Syntax of switch keyword:

switch(block) {      
case number1:      
//block of code or statements.      
 break; //optional    
case value2:      
//block of code that should be executed.      
break; //optional    
default:       
The default code is executed if any of the cases are not matched or executed.      
}     

Examples of Java switch keyword:

Example 1: Printing the string values using the switch label.

public class SwitchEx1 {  
    public static void main (String [] s) {  


        String No._of_weeks_in_month="Four”. 
        switch (No._of_weeks_in_month)  


        {  
        case "Four": System.out.println("Four");


        }  
    }  
}  

Output of the program is:

Four

Example 2: Using multiple case labels in the java program.

public class SwitchEx2 {  
    public static void main (String [] args) {  
            String Scales="SA";
switch (Scales)   
        {  
        case "SA":   
            System.out.println("SA").  
            break.  
        case "RE":  
            System.out.println("RE").  
            break.  
        case "GA":  
            System.out.println("GA").  
            break.  
        case "MA":   
            System.out.println("MA").  
break;
        case "PA":  
            System.out.println("PA");
break;
        case "DA":   
            System.out.println("DA");
            break.  
        case "NE":  
            System.out.println("NE");
            break.  
        default:  
            System.out.println("nothing printed");
        }  
    }  
}  

Output:

SA

Example 3: printing the value of int using the switch label

public class SwitchEx3 {  


    public static void main (String [] args) {  


        int age=50;


        switch(age)  


        {  
        case 5:   
            System.out.println("The age is 5");
break;
        case 35:  
            System.out.println("The age is 35");
break;
        case 50:  
            System.out.println("The age is 50");
break;


        default:  
            System.out.println("No age found");
        }  
    }  
}  

Output:

 The age is 50.

Example 4: case label holding switch statement

public class switchEx4 {  


    public static void main (String [] args) {  


        String clg_name="bvrit”.  
        int dept_id=189.  


        switch(clg_name)  


        {  
        case "bvrit":   
            System.out.println("bvrit");
            switch(dept_id)  
            {  
            case 185:  
                System.out.println("information technology departement ");
break;
            case 186:  
                System.out.println("Computer science Department");
break;
            case 187:  
                System.out.println("Civil Department");
break;
            case 188:  
                System.out.println("mechanical Department");
break;
            case 189:  
                System.out.println("electronics Department");
break;
            case 190:  
                System.out.println("Electrical Department");
break;


            }  
break;
        case "JNTU":  
            System.out.println("JNTU");
            switch(dept_id)  
            {  
            case 101:  
                System.out.println("Mech Department");
break;
            case 102:  
                System.out.println("Computer science Department");
break;
            case 103:  
                System.out.println("Civil Department");
break;
            }  
break;
        case "Lpu":  
            System.out.println("Lpu");
            switch(dept_id)  
            {  
            case 115:  
                System.out.println("Mec Department");
break;
            case 118:  
                System.out.println("Computer science Department");
break;
            case 201:  
                System.out.println("Civil Department");
break;
            }  
break;


default :
            System.out.println("Try once again");
        }  
    }  
}  

Output:

Lpu
Computer science Department

Example 5: Calculate the weekday name using switch label:

int day = 3;
switch (day of the week) {
  case 1:
    System.out.println("Mon");
break;
  case 2:
    System.out.println("Tue");
break;
  case 3:
    System.out.println("Wed");
break;
  case 4:
    System.out.println("Thur");
break;
  case 5:
    System.out.println("Fri");
break;
  case 6:
    System.out.println("Sat");
break;
  case 7:
    System.out.println("Sun");
break;
}

Output:

 " Wed " (day 3)

Example 6: Nested switch keyword Example:

to Demonstrate
// Switch Case Statement using the nested


// Class
public class nested {
    public static void main (String [] args)
    {
        // input value
        String Group = "IT";
        int semester = 1;


        // Switch case
        switch (semester) 
        case 1:


            // Switch inside a switch
            // Nested Switch
            switch (Group) {


            // Nested case
            case "IT":
            case "ECE":
                System.out.println(
                    "Elective courses: Electronics, Mtlab");
                break.


            // Case
            case "EEE":
                System.out.println(
                    "Elective courses: Antenna Engineering");
                break.


                // default case
                // It will execute if above cases does not
                // execute
            default:


                // Print statement
                System.out.println(
                    "Elective courses: answering or optimization");
            }
        }
    }
}

Output:

elective courses: Electronics, Mtlab.

Related Topics

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 minutes read.