Skip to main content

Switch Statement in Java:

 

Switch statement in Java programming is a conditional solution which will allow the programmer to execute a particular code according to the conditions set behind.

Switch statement is an alternative to if statement. It is even a better alternative to if statement.

If you have to check multiple conditions and evaluate one statement, in this case Java if statement is not an ideal solution. Switch case statement is perhaps the right programming solution to it.

It selects one of the many code blocks and executes the same. This programming allows the control flow to switch from one statement to another until the case validation process returns a true value.

After every statement, there is a ‘break keyword’ which allows the control flow to get out of switch statement and proceed to the next line.

 

The syntax of Java switch-case statement:

 

switch(expression) {

       

        case 1:

             //Code goes here

             break;

        case 2:

             //Code goes here

             break;

        case 3:

             //Code goes here

             break;

        default:

//Code goes here           

        }   

           

 

In the above syntax, the ‘switch’ expression is evaluated at the beginning to compare the expression with each and every case.

If the expression is getting matched with any one of the cases, the corresponding code block will be executed.

The ‘break keyword’ plays a very crucial role in Java ‘switch case statement’. As soon as any one of the cases matches with the expression and the corresponding code block execution happens, the ‘break keyword’ will immediately through the java control flow out of the java ‘switch statement’ and the switch function will immediately be terminated.

There is a ‘default case’ set at the bottom of the ‘switch case statement’. As the name suggests, it is default for the all other cases apart from the matching cases.

The ‘default case’ will only be in action when the expression is not matching with any one of the mentioned cases. The code block inside the ‘default case’ will be executed when the java control arrives at the ‘default switch case’.

 

Programming example of Java switch statement:

 

 

 package java_lesson;

 

import java.util.Scanner;

 

public class switch_statement {

 

    public static void main(String[] args) {

        

         Scanner sc = new Scanner(System.in);

        

         int month_serial_no;

         System.out.println("Enter a number between 1 to 12 to see corresponding month");

         month_serial_no = sc.nextInt();

        

         switch(month_serial_no) {

        

         case 1:

              System.out.println("1 is equivalent to January");

              break;

         case 2:

              System.out.println("2 is equivalent to February");

              break;

         case 3:

              System.out.println("3 is equivalent to March");

              break;

         case 4:

              System.out.println("4 is equivalent to April");

              break;

         case 5:

              System.out.println("5 is equivalent to May");

              break;

         case 6:

              System.out.println("6 is equivalent to June");

              break;

         case 7:

              System.out.println("7 is equivalent to July");

              break;

         case 8:

              System.out.println("8 is equivalent to August");

              break;

         case 9:

              System.out.println("9 is equivalent to September");

              break;

         case 10:

              System.out.println("10 is equivalent to October");

              break;

         case 11:

              System.out.println("11 is equivalent to November");

              break;

         case 12:

              System.out.println("12 is equivalent to December");

              break;

         default:

              System.out.println("Wrong Input! Please enter a number between 1 - 12 only.");

             

         }   

 

    }

 

}

 

 

 

Output in the Console:

Enter a number between 1 to 12 to see corresponding month

7

7 is equivalent to July

 

 

Since the ‘default case’ statement is used at the end of the ‘switch case’ statement, there is no need to use ‘break keyword’ in default case.

 

Sum Up:

Hope, after learning about Switch case statement, you did not find it difficult at all. This lesion is very important in Java programming and there is a lot of practical use of Java switch case statement in professional world programming.

Comments

Popular posts from this blog

What is Java

Java is a general purpose programming language. Which means this programming language is used in many purposes apart from being used only in android application development. There are a few similarities between Java programming language and C++ programming language. But Java programming language is having some advanced features which you are going to learn in future. In programming languages you write programs in the form of statements in many lines and those statements are executed one by one. In Java, the execution happens really fast and concurrently it can execute many statements. Java is a platform independent (Java compiled byte code can be run across different operating systems) and object oriented programming language.   Why should we learn Java programming language?   Easy to learn: Java is a easy to learn programming language. Once you start learning Java programming, you will actually be learning the real programming and you will observe everyth...

Java User Input Exercise

Exercise 1: Write a simple java program to take user input of a number and store it inside an integer variable. Later print the number on the screen.   Solution 1:   import java.util.Scanner;   public class UserInpur {        public static void main(String[] args ) {               Scanner sc = new Scanner(System. in );           System. out .println( "Enter a number" );           int x = sc .nextInt();           System. out .println( "You have entered:" );                   System. out .println( x );        }   }   Exercise 2: Write a simple java program to tak...

Java Operators

In this lesson you are goin g to learn about operators in Java and also what are the different types of operators used in Java programming.   In our previous lesson we have learnt how to take user input in Java. We were performing different types of arithmetic operations in order to create a simple ration shop billing application which took different values as the user input and calculated total billing amount.   During the programming the billing application we used different signs like addition (+) , multiplication (*) etc. These signs are called operators in Java programming.   Types of Operators in Java There are five types of operators in Java. Arithmetic Operators Bitwise Operators Assignment Operators Comparison Operators Logical Operators 1.Java Arithmetic Operators Arithmetic operators are used to perform mathematical operations such addition, division, multiplication, subtraction and modulation. Let’s see the example of arithmetic operators in below:   ...