×

Java Case Keyword

Case keyword:

  • In this article we are going to learn the concept of a java case keyword.
  • Generally, java case keyword is used with the switch statements.
  • Case 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 a 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 helps 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 Case 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 Case keyword

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

public class CaseEx1 {  
    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:

Four

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

public class CaseEx2 {  
    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 case label

public class CaseEx3 {  


    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 CaseEx4 {  


    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 technologydepartement ");  
                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 case 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)

EXAMPLE6: Nested case 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
        case 1:
            System.out.println(
                "Electivecourses: Advance calculus, integration");


            // Break statement to hault execution here
            // itself if case is matched
break.


            // Case
        case 2:


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


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


            // Case
            case "EEE":
                System.out.println(
                    "Electivecourses: 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

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Java Thread Lifecycle: States and Stages

Multithreading Multithreading is one of the most important features in object-oriented programming, and this multithreading can be implemented by various object-oriented programming languages like Java, Python, and C++. A multithreaded process...

7 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 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.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.