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.

Switch Statement in Java:  Switch Statement in Java: Reviewed by Technobits on April 01, 2021 Rating: 5

No comments:

Powered by Blogger.