Select Case Statement in Java

This tutorial will teach you how to use the Select Case Statement in Java. A switch statement checks if a variable is equal to a list of values, meaning it will hold the value of the variable. This is the syntax for the Select-Case Statement:
  1. switch(expression){
  2.     case value of your condition:
  3.        //Code Statements
  4.        break; //optional
  5.     case value of your condition :
  6.        //Code Statements
  7.        break; //optional
  8.     //You can have any number of case statements.
  9.     default : //Optional
  10.        //Statements
  11. }
So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of variables.java. 2. Import the io library:
  1. import java.io.*;
3. Now, we will initialize variable month as Integer in the Main and in as our inputting.
  1.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  2.         int Month;
  3.                
  4.         System.out.print("Enter Month Number: ");
  5.         Month =Integer.parseInt(in.readLine());
4. Now, we will create a Select Case Statement. Have this code below:
  1.         switch(Month){
  2.                
  3.                 case 1:
  4.                
  5.                         System.out.println("Month of January");break;
  6.                        
  7.                 case 2:
  8.                
  9.                         System.out.println("Month of February");break;
  10.                
  11.                 case 3:
  12.                        
  13.                         System.out.println("Month of March");break;
  14.                 case 4:
  15.                        
  16.                         System.out.println("Month of April");break;
  17.                 case 5:
  18.                
  19.                         System.out.println("Month of May");break;
  20.                 case 6:
  21.                
  22.                         System.out.println("Month of June");break;
  23.                 case 7:
  24.                        
  25.                         System.out.println("Month of July");break;
  26.                 case 8:
  27.                
  28.                         System.out.println("Month of August");break;
  29.                 case 9:
  30.                
  31.                         System.out.println("Month of September");break;
  32.                 case 10:
  33.                
  34.                         System.out.println("Month of October");break;
  35.                 case 11:
  36.                                                
  37.                         System.out.println("Month ofNovember");break;
  38.                 case 12:
  39.                
  40.                         System.out.println("Month ofDecember");break;
  41.                 default:
  42.                
  43.                         System.out.println("INVALID INPUT");                                           
  44.         }
As you have seen the code above, the variable Month was used to get the user input. The case 1 to 12 here are the possible input numbers of the user. Hence, if you will input 1, it will display "Month of January". The break keyword here breaks the case of its code statement for this to not to go to another case statement. We used the keyword default if the inputted number is not on the case, then the statement under default will be executed. Hence, if you will input number 13 and 13 is not on the case, the output will display " INVALID INPUT". Output: output Here's the full code of this tutorial:
  1. import java.io.*;
  2. public class selectCase{
  3.  
  4.    public static void main(String args[]) throws IOException{
  5.        
  6.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  7.        
  8.         int Month;
  9.                
  10.         System.out.print("Enter Month Number: ");
  11.         Month =Integer.parseInt(in.readLine());
  12.        
  13.    
  14.         switch(Month){
  15.                
  16.                 case 1:
  17.                
  18.                         System.out.println("Month of January");break;
  19.                        
  20.                 case 2:
  21.                
  22.                         System.out.println("Month of February");break;
  23.                
  24.                 case 3:
  25.                        
  26.                         System.out.println("Month of March");break;
  27.                 case 4:
  28.                        
  29.                         System.out.println("Month of April");break;
  30.                 case 5:
  31.                
  32.                         System.out.println("Month of May");break;
  33.                 case 6:
  34.                
  35.                         System.out.println("Month of June");break;
  36.                 case 7:
  37.                        
  38.                         System.out.println("Month of July");break;
  39.                 case 8:
  40.                
  41.                         System.out.println("Month of August");break;
  42.                 case 9:
  43.                
  44.                         System.out.println("Month of September");break;
  45.                 case 10:
  46.                
  47.                         System.out.println("Month of October");break;
  48.                 case 11:
  49.                                                
  50.                         System.out.println("Month ofNovember");break;
  51.                 case 12:
  52.                
  53.                         System.out.println("Month ofDecember");break;
  54.                 default:
  55.                
  56.                         System.out.println("INVALID INPUT");                                           
  57.         }
  58.        
  59.        
  60.        
  61.        
  62.    }
  63.    
  64.    
  65. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment