Codeforcoding

Switch case statement in Java language

Switch case statement in Java language

In this tutorial, we are going to discuss the Switch case statement in Java language

Switch statements in Java select one option from multiple options of cases based on a switch expression.

The switch statement is similar to if else if … else ladder. but it is not as same as if else if…else. The switch case is used to execute a block of code from many options

The following syntax of the switch statement to clearly depicts the flow of switch statements.

The syntax of Switch statements

Syntax

switch(expression)
{
case constant_1:
//block of statements 1   
         //code to be executed, if the expression is matched to  constant_1(case 1)
break;
case constant_2:
//block of statements 2  
         //code to be executed, if the expression is matched to  constant_2(case 2)
break;

case constant_3:
//block of statements 3  
        //code to be executed, if the expression is matched to  constant_3(case 3)
break;

default:
  default block; 
        //code to be executed, if the expression doesn't matched to  any constant_1(case 1)
break;

}

In the switch case statement, when a case statement is matched with the switch expression, the flow of control of the program will pass the block of code associated with case statements

Switch Statement Flowchart

C program switch case statement
C program switch case

 

Program 1

class SwitchCaseJava{
public static void main(String args[]){
int age=30;
    switch(age){
case 20:
    System.out.print("age is equal to 20");
    break;

case 22:
     System.out.print("age is equal to 22");
    break;

case 24:
     System.out.print("age is equal to 24");
    break;

case 26:
     System.out.print("age is equal to 26");
    break;
default:
     System.out.print("age is not equal to any value");
    }
}
}

When the above code executed, it produces the following results

age is not equal to any value

In the above program, if the age is defined as 30. the flow of control checks every case to find an alternative case. Then the flow of control fails to find the associated case . the default statement will be displayed as follows

 

Program 2

import java.util.Scanner;
class SwitchCaseInJava1{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of month: ");

int month=scan.nextInt();


    switch(month){
case 1:
    System.out.print("This month is January");
    break;

    case 2:
    System.out.print("This month is February");
    break;

    case 3:
    System.out.print("This month is march");
    break;

    case 4:
    System.out.print("This month is april");
    break;

    case 5:
    System.out.print("This month is May");
    break;

    case 6:
    System.out.print("This month is June");
    break;

    default:
        System.out.print("your month is not available in first half months");
    break;
    }
    }
    }

When the above code executed, it produces the following results

Enter the number of month: 5
This month is May

 

Points to remember of the switch case statements

 

Break statements in Switch case

The break statement is optional of switch case but when we use break statements of switch statemeent, we can get clear output with the associated case.

let’s see the example below where I am not using the break statements

Program

public class SwitchCaseExInJava{
public static void main(String args[]){
int num=30;
switch(num){
case 10:
System.out.println("Ten");

case 20:
System.out.println("Twenty");

case 30:
System.out.println("Thirty");//associated case

case 40:
System.out.println("Fourty");

case 50:
System.out.println("Fifty");

Default:
 System.out.println("No match any cases");
}


}


}

When the above code executed, it produces the following results

Thirty 
Fourty
Fifty
No match any cases

 

Related keywords

Switch keyword in Java language

Break keyword in Java language

Default keyword in Java language

 

Suggested for you

If statements in Java

Switch statements in C

Switch statements in C++

For loop in Java language

While loop in Java language

 

Loops in Java programming language
switch case statement in C language
Exit mobile version