×

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 Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.