Switch Case in Java




  • The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch-case-default.

  • The Switch executes the case where a match is found and then all subsequent cases till end, so stop that fall through we use break; statement in each case.

  • A switch works with the byte, short, char, and int primitive data types.

  • It also works with enumerated types , the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.

Syntax :

switch(expression){
   case value1  :
      statement(s);
      break; //optional
   case value2  :
      statement(s);
      break; //optional
    .....
/* you can have any number of case statements. */
   default : //Optional
      statement(s);
  /* code to be executed if all cases are not matched */
}               

Block Diagram :

Example:

WAP to give choice to user from menu to perform action.
import java.util.Scanner;
class Test
{
 public static void main(String []arg)
 {
  Scanner sc;
  sc = new Scanner(System.in);
  int x ;
  System.out.println("MENU\n----------");
  System.out.println("1. for Hello");
  System.out.println("2. for Bye");
  System.out.println("-----------");
  System.out.print("Enter Your Choice: ");
  x = sc.nextInt();
 switch(x) {
  case 1: System.out.print("HELLO USER!!)";
 	  break;
  case 2: System.out.print("BYE USER!!");
	  break;
  default: System.out.print("INVALID!!");
  }//switch closed
 }//main closed
}//class closed
OUTPUT:
MENU
-----------
1. for Hello
2. for Bye
-----------
Enter Your Choice: 1
HELLO USER!!

Using Strings in switch :

In Java SE 7 and later, you can use a String object in the switch statement's expression. The following code example, StringSwitchDemo, displays the number of the month based on the value of the String named month:

public class StringSwitchDemo {

 public static void main(String[] args) 
 {
  String month = "august";
  switch (month)
 {
   case "january":
        System.out.print("1st Month");
        break;
   case "february":
        System.out.print("2nd Month");
        break;
   case "march":
        System.out.print("3rd Month");
        reak;
   case "april":
        System.out.print("4th Month");
        break;
   case "may":
        System.out.print("5th Month");
        break;
   case "june":
        System.out.print("6th Month");
        break;
   case "july":
        System.out.print("7th Month");
        break;
   case "august":
        System.out.print("8th Month");
        break;
   case "september":
        System.out.print("9th Month");
        break;
   case "october":
        System.out.print("10th Month");
        break;
   case "november":
        System.out.print("11th Month");
        break;
   case "december":
        System.out.print("12th Month");
        break;
   default: 
        System.out.print("wrong Month");
        break;
  }//switch closed
 }//main closed
}//class closed
 8th Month   

Next topic is Java-Loops



 







Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved