Java Conditional Statement

Switch Case Statement

The switch statement in java is a multiway branch statement. A switch-case statement in programming allows you to choose one of multiple code blocks for execution based on the value of an expression. It offers a clearer and more organized approach compared to using multiple if-else-if statements. This is especially helpful when you have a single expression that needs to be compared against several conditions to determine which block of code to execute.

Syntax:

switch (expression) {
case value1:
// code block to be executed if expression matches value1
break;
case value2:
// code block to be executed if expression matches value2
break;
// additional cases...
default:
// code block to be executed if expression doesn't match any case
break;
}

Working:

  • The value of the expression is compared with each of the literal values in the case statements.If a match is found,the code sequence following that case statement is executed.
  • If none of the constants matches the value of the expression, then the default statement is executed.
  • If no case match and no default is present,then no further action is taken.
  • Break: The break is used inside the switch to terminate a statement sequence.


Note: The expression must be of type byte,short,int, or char; each of the values specified in the case must be of type compatible with the expression.


Note:Each case value must be a unique literal. Duplicate case value is not allowed.


Note:A switch statement is usually more efficient then a set of nested ifs.

#Java programs to demonstrate switch case statement

Program-1:

import java.util.Scanner;
     public class ApnaStudy { // Improved class name
     public static void main(String[] args) {
     Scanner scanner = new Scanner(System.in);

     System.out.println("Enter the month number (1-12):");
     int month = scanner.nextInt();

     switch (month) {
     case 1:
     System.out.println("The first month is January.");
     break;
     case 2:
     System.out.println("The second month is February.");
     break;
     case 3:      System.out.println("The third month is March.");
     break;
     case 4:
     System.out.println("The fourth month is April.");
     break;
     case 5:
     System.out.println("The fifth month is May.");
     break;
     case 6:
     System.out.println("The sixth month is June.");
     break;
     case 7:
     System.out.println("The seventh month is July.");
     break;
     case 8:
     System.out.println("The eighth month is August.");
     break;
     case 9:
     System.out.println("The ninth month is September.");
     break;
     case 10:
     System.out.println("The tenth month is October.");
     break;
     case 11:
     System.out.println("The eleventh month is November.");
     break;
     case 12:
     System.out.println("The twelfth month is December.");
     break;
     default:
     System.out.println("Invalid month number. Please enter a number between 1 and 12.");
     }
     scanner.close(); // Close the scanner to avoid resource leaks
    }
}


/*OUTPUT*/

Enter the day number (1-7):
5
It's Friday! Time for the weekend!

Program-2:

import java.util.Scanner;

     public class ApnaStudy {

     public static void main(String[] args) {
     Scanner scanner = new Scanner(System.in);
     System.out.println("Enter the day number (1-7):");
     int day = scanner.nextInt();

     switch (day) {
     case 1:
     System.out.println("It's Monday! Time for a fresh start.");
     break;
     case 2:
     System.out.println("It's Tuesday! Just keep pushing through.");
     break;
     case 3:
     System.out.println("It's Wednesday! Hump day!");
     break;
     case 4:
     System.out.println("It's Thursday! Almost there...");
     break;
     case 5:
     System.out.println("It's Friday! Time for the weekend!");
     break;
     case 6:
     System.out.println("It's Saturday! Relax and recharge.");
     break;
     case 7:
     System.out.println("It's Sunday! Enjoy your well-deserved rest.");
     break;
     default:
     System.out.println("Invalid day number. Please enter a number between 1 and 7.");
     }

     scanner.close();
    }
}


/*OUTPUT*/

Enter the marks obtained (0-100):
50
Your grade is: E

Previous Next