Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Switch case in java

 
 

Share on Google+Share on Google+

Switch case in java

Advertisement
Switch statement is a control statement that allow multiple selection by passing control to one of the case statement in the body.

Switch case in java

In this section we will learn about switch case in java. In most of the high level programming languages such as C, C++, Pascal, and java, switch case is used . switch  is a selection control mechanism used to allow the variable to change the control flow of the program. Unlike if statement, switch allow the user to select a option from multiple options. switch statement have multiple number of possible execution path. It work with byte, short, char and int primitive data types. The following code will illustrate the use of switch case in java:

import java.util.Scanner;
public class Switch {

	public static void main(String args[])
	{
	    System.out.println("Switch Example");
	    System.out.println("Press day of the week");
	    Scanner sc =new Scanner(System.in);
	    int days=sc.nextInt();
	    switch(days)
	    {
	    case 1:System.out.println("Today is Monday");
	               break;
	    case 2:System.out.println("Today is Tuesday");
	               break;  
	    case 3:System.out.println("Today is Wednesday");
                   break;
	    case 4:System.out.println("Today is Thursday");
                   break;
	    case 5:System.out.println("Today is Friday");
                   break;
	    case 6:System.out.println("Today is Saturday");
                   break;
	    case 7:System.out.println("Today is Sunday");
                   break;
	    
         default:System.out.println("Invalid option");
                   break;
	    }
	   
	}
   }

Output : When you compile and execute this code, output will look like this :

Description : In the above code switch case have multiple option unlike if else case, depending on the user choice it will display the day of the week. Through scanner class reading the input from the user and passing that variable to switch case depending on the choice of user. The body of the switch statement is called switch block. A statement in switch is labeled with one or more case or default. If we talk about the advantage of switch over if else then the same code could be written as follows:

import java.util.Scanner;
public class ExampleD {

public static void main(String args[])
{
	System.out.println("Day of the week");
	System.out.println("Press day of the week");
    Scanner sc =new Scanner(System.in);
    int days=sc.nextInt();
    if(days==1)
    System.out.println("Today is Monday");
    else if(days==2)
    	System.out.println("Today is Tuesday");
    else if(days==3)
    	System.out.println("Today is Wednesday");
    else if(days==4)
    	System.out.println("Today is Thursday");
    else if(days==5)
    	System.out.println("Today is Friday");
    else if(days==6)
    	System.out.println("Today is Saturday");
    else if(days==7)
    	System.out.println("Today is Sunday");
    else{
    	System.out.println("Invalid option");
    }
  }
}

So, for writing the same code with if statement so many if else statement would be written. To overcome this problem Switch is used.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 10, 2013

Related Tutorials

Discuss: Switch case in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com