Java case Keyword

Java Case Keyword


Case Keyword In Java
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. 
The case is a predefined keyword hence it will be written in small cases only.
1. We can not repeat the case value.
2. We can not use a floating constant with the case.
3. Case value has no sequence.
Here is an example - 
Example -
Here’s an example that takes a day number from 1 to 7, and determines the day name.
import java.util.*;
public class VowelsAndConsonants { public static void main(String[] args) {
int dayno;
System.out.println("Input Day No : ");
Scanner ob = new Scanner(System.in);
dayno = ob.nextInt();
  switch(dayno)
  {
case 1:
      System.out.println("Monday);
      break;
case 2:
      System.out.println("Tuesday ");
      break;
case 3:
      System.out.println("Wednesday ");
      break;
case 4:
      System.out.println("Thursday");
      break;
case 5:
      System.out.println("Friday ");
      break;
case 6:
  System.out.println("Saturday ");
      break;
case 7:
      System.out.println("Sunday ");
      break;
default:
      System.out.println("Invalid day number. \nPlease try again ....");
      break;
      }
}
 
Output-
Input Day No : 5                                                                                              
Friday