E2EHIRING Logo
search

Search blogs by title

Jobs
Jobs
internships
Internships
Company
Partners
mentorship
Mentorship
more
Moredropdown
Login
HomeSepratorIconBlogsSepratorIconSwitch Case in java .SepratorIcon

Switch Case in java .

Han Soloshahbaz peerzade
calendar11 Jul 2022
poster

What is a Switch Case in Java?

The switch statement or switch case in java is a multi-way branch statement. Based on the value of the expression given, different parts of code can be executed quickly. The given expression can be of a primitive data type such as int, char, short, byte, and char. With JDK7, the switch case in java works with the string and wrapper class and enumerated data type (enum in java).

Rules to Remember

 certain points to remember while writing a switch case in java.  

  • We cannot declare duplicate values in a switch case.
  • The values in the case and the data type of the variable in a switch case must be same.
  • Variables are not allowed in a case, it must be a constant or a literal.
  • The break statement fulfills the purpose of terminating the sequence during execution.
  • It is not necessary to include the break statement, the execution will move to the next statement if the break statement is missing.
  • The default statement is optional as well, it can appear anywhere in the block. 

This is how it works:

  • The switch expression is evaluated once.
  • The value of the expression is compared with value of each case.
  • If there is a match, the associated block of code is executed.
  • The break and default keyword are optional, 


// Syntax        

            switch(expression)   {
                     case 1 :
                                        //statement
                  break;
                  case 2   :
                                       //statement
                 break;
                  default:
                                      //statement
            }


Flow Chart

                                                 


 Program
public static void main(String[] args) {    
            char ch='A';   
            switch(ch)  
            {  
                case 'a':   
                    System.out.println("Vowel");  
                    break;  
                case 'e':   
                    System.out.println("Vowel");  
                    break;  
                case 'i':   
                    System.out.println("Vowel");  
                    break;  
                case 'o':   
                    System.out.println("Vowel");  
                    break;  
                case 'u':   
                    System.out.println("Vowel");  
                    break;  
                case 'A':   
                    System.out.println("Vowel");  
                    break;  
                case 'E':   
                    System.out.println("Vowel");  
                    break;  
                case 'I':   
                    System.out.println("Vowel");  
                    break;  
                case 'O':   
                    System.out.println("Vowel");  
                    break;  
                case 'U':   
                    System.out.println("Vowel");  
                    break;  
                default:   
                    System.out.println("Consonant");  
            }  
        }
 output // Vowel

 

The break Keyword

When Java reaches a break keyword, it breaks out of the switch block.

This will stop the execution of more code and case testing inside the block.

When a match is found, and the job is done, it's time for a break. There is no need for more testing.

The default Keyword

The default keyword specifies some code to run if there is no matching found.

In this article, we have discussed how we can use switch case in java with various examples. With the use of conditional statements it becomes easier to test multiple conditions at once and also generate an optimized solution of rather difficult problem

Recent Posts

Innovation is the Need of the Hour: Else you are Outdated

Innovation is the Need of the Hour: Else you are Outdated

Router In React

Router In React

Higher Order Component in React

Higher Order Component in React

Styled Component in React

Styled Component in React

Spring - Bean Life Cycle

Spring - Bean Life Cycle

copycopycopycopy

Han Solo

Recent Posts

Innovation is the Need of the Hour: Else you are Outdated

Innovation is the Need of the Hour: Else you are Outdated

Router In React

Router In React

Higher Order Component in React

Higher Order Component in React

Styled Component in React

Styled Component in React

Spring - Bean Life Cycle

Spring - Bean Life Cycle

e2e logo