Select Case Statement in Java
Submitted by donbermoy on Monday, December 29, 2014 - 11:37.
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:
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:
3. Now, we will initialize variable month as Integer in the Main and in as our inputting.
4. Now, we will create a Select Case Statement. Have this code below:
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:
Here's the full code of this tutorial:
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
- switch(expression){
- case value of your condition:
- //Code Statements
- break; //optional
- case value of your condition :
- //Code Statements
- break; //optional
- //You can have any number of case statements.
- default : //Optional
- //Statements
- }
- import java.io.*;
- int Month;
- switch(Month){
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- case 8:
- case 9:
- case 10:
- case 11:
- case 12:
- default:
- }

- import java.io.*;
- public class selectCase{
- int Month;
- switch(Month){
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- case 8:
- case 9:
- case 10:
- case 11:
- case 12:
- default:
- }
- }
- }
Add new comment
- 871 views