Switch Statement
In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. The switch statement is easier to use instead of if-else-if statements. It also enhances the readability of the program.
Points to be noted about switch statement:
- The case variables can be int, short, byte, char, or enumeration. String type is also supported since version 7 of Java
- Cases cannot be duplicate
- Default statement is executed when any of the case doesn't match the value of expression. It is optional.
- Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed. - While using switch statements, we must notice that the case expression will be of the same type as the variable. However, it will also be a constant value.
The syntax to use the switch statement is given below.
- switch (expression){
- case value1:
- statement1;
- break;
- .
- .
- .
- case valueN:
- statementN;
- break;
- default:
- default statement;
- }
Consider the following example to understand the flow of the switch statement.
Student.java
- public class Student implements Cloneable {
- public static void main(String[] args) {
- int num = 2;
- switch (num){
- case 0:
- System.out.println("number is 0");
- break;
- case 1:
- System.out.println("number is 1");
- break;
- default:
- System.out.println(num);
- }
- }
- }
Output:
While using switch statements, we must notice that the case expression will be of the same type as the variable. However, it will also be a constant value. The switch permits only int, string, and Enum type variables to be used.
Java Switch Statement
The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch statement.
In other words, the switch statement tests the equality of a variable against multiple values.
Points to Remember
- There can be one or N number of case values for a switch expression.
- The case value must be of switch expression type only. The case value must be literal or constant. It doesn't allow variables.
- The case values must be unique. In case of duplicate value, it renders compile-time error.
- The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string.
- Each case statement can have a break statement which is optional. When control reaches to the break statement, it jumps the control after the switch expression. If a break statement is not found, it executes the next case.
- The case value can have a default label which is optional.
Syntax:
- switch(expression){
- case value1:
-
- break;
- case value2:
-
- break;
- ......
-
- default:
- code to be executed if all cases are not matched;
- }
Flowchart of Switch Statement
Example:
SwitchExample.java
- public class SwitchExample {
- public static void main(String[] args) {
-
- int number=20;
-
- switch(number){
-
- case 10: System.out.println("10");
- break;
- case 20: System.out.println("20");
- break;
- case 30: System.out.println("30");
- break;
-
- default:System.out.println("Not in 10, 20 or 30");
- }
- }
- }
Output:
Finding Month Example:
SwitchMonthExample.javaHTML
-
-
- public class SwitchMonthExample {
- public static void main(String[] args) {
-
- int month=7;
- String monthString="";
-
- switch(month){
-
- case 1: monthString="1 - January";
- break;
- case 2: monthString="2 - February";
- break;
- case 3: monthString="3 - March";
- break;
- case 4: monthString="4 - April";
- break;
- case 5: monthString="5 - May";
- break;
- case 6: monthString="6 - June";
- break;
- case 7: monthString="7 - July";
- break;
- case 8: monthString="8 - August";
- break;
- case 9: monthString="9 - September";
- break;
- case 10: monthString="10 - October";
- break;
- case 11: monthString="11 - November";
- break;
- case 12: monthString="12 - December";
- break;
- default:System.out.println("Invalid Month!");
- }
-
- System.out.println(monthString);
- }
- }
Output:
Program to check Vowel or Consonant:
If the character is A, E, I, O, or U, it is vowel otherwise consonant. It is not case-sensitive.
SwitchVowelExample.java
- public class SwitchVowelExample {
- public static void main(String[] args) {
- char ch='O';
- switch(ch)
- {
- case 'a':
- System.out.println("Vowel");
- break;
- case 'e':
- System.out.println("Vowel");
- break;
- case 'i':
- System.out.println("Vowel");
- break;
- case 'o':
- System.out.println("Vowel");
- break;
- case 'u':
- System.out.println("Vowel");
- break;
- case 'A':
- System.out.println("Vowel");
- break;
- case 'E':
- System.out.println("Vowel");
- break;
- case 'I':
- System.out.println("Vowel");
- break;
- case 'O':
- System.out.println("Vowel");
- break;
- case 'U':
- System.out.println("Vowel");
- break;
- default:
- System.out.println("Consonant");
- }
- }
- }
Output:
Java Switch Statement is fall-through
The Java switch statement is fall-through. It means it executes all statements after the first match if a break statement is not present.
Example:
SwitchExample2.java
-
-
- public class SwitchExample2 {
- public static void main(String[] args) {
- int number=20;
-
- switch(number){
-
- case 10: System.out.println("10");
- case 20: System.out.println("20");
- case 30: System.out.println("30");
- default:System.out.println("Not in 10, 20 or 30");
- }
- }
- }
Output:
20
30
Not in 10, 20 or 30
Java Switch Statement with String
Java allows us to use strings in switch expression since Java SE 7. The case statement should be string literal.
Example:
SwitchStringExample.java
-
-
- public class SwitchStringExample {
- public static void main(String[] args) {
-
- String levelString="Expert";
- int level=0;
-
- switch(levelString){
-
- case "Beginner": level=1;
- break;
- case "Intermediate": level=2;
- break;
- case "Expert": level=3;
- break;
- default: level=0;
- break;
- }
- System.out.println("Your Level is: "+level);
- }
- }
Output:
Java Nested Switch Statement
We can use switch statement inside other switch statement in Java. It is known as nested switch statement.
Example:
NestedSwitchExample.java
-
- public class NestedSwitchExample {
- public static void main(String args[])
- {
-
- char branch = 'C';
- int collegeYear = 4;
- switch( collegeYear )
- {
- case 1:
- System.out.println("English, Maths, Science");
- break;
- case 2:
- switch( branch )
- {
- case 'C':
- System.out.println("Operating System, Java, Data Structure");
- break;
- case 'E':
- System.out.println("Micro processors, Logic switching theory");
- break;
- case 'M':
- System.out.println("Drawing, Manufacturing Machines");
- break;
- }
- break;
- case 3:
- switch( branch )
- {
- case 'C':
- System.out.println("Computer Organization, MultiMedia");
- break;
- case 'E':
- System.out.println("Fundamentals of Logic Design, Microelectronics");
- break;
- case 'M':
- System.out.println("Internal Combustion Engines, Mechanical Vibration");
- break;
- }
- break;
- case 4:
- switch( branch )
- {
- case 'C':
- System.out.println("Data Communication and Networks, MultiMedia");
- break;
- case 'E':
- System.out.println("Embedded System, Image Processing");
- break;
- case 'M':
- System.out.println("Production Technology, Thermal Engineering");
- break;
- }
- break;
- }
- }
- }
Output:
Data Communication and Networks, MultiMedia
Java Enum in Switch Statement
Java allows us to use enum in switch statement. Java enum is a class that represent the group of constants. (immutable such as final variables). We use the keyword enum and put the constants in curly braces separated by comma.
Example:
JavaSwitchEnumExample.java
-
-
- public class JavaSwitchEnumExample {
- public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
- public static void main(String args[])
- {
- Day[] DayNow = Day.values();
- for (Day Now : DayNow)
- {
- switch (Now)
- {
- case Sun:
- System.out.println("Sunday");
- break;
- case Mon:
- System.out.println("Monday");
- break;
- case Tue:
- System.out.println("Tuesday");
- break;
- case Wed:
- System.out.println("Wednesday");
- break;
- case Thu:
- System.out.println("Thursday");
- break;
- case Fri:
- System.out.println("Friday");
- break;
- case Sat:
- System.out.println("Saturday");
- break;
- }
- }
- }
- }
Output:
Sunday
Monday
Twesday
Wednesday
Thursday
Friday
Saturday
Java Wrapper in Switch Statement
Java allows us to use four wrapper classes: Byte, Short, Integer and Long in switch statement.
Example:
WrapperInSwitchCaseExample.java
-
-
- public class WrapperInSwitchCaseExample {
- public static void main(String args[])
- {
- Integer age = 18;
- switch (age)
- {
- case (16):
- System.out.println("You are under 18.");
- break;
- case (18):
- System.out.println("You are eligible for vote.");
- break;
- case (65):
- System.out.println("You are senior citizen.");
- break;
- default:
- System.out.println("Please give the valid age.");
- break;
- }
- }
- }
Output:
You are eligible for vote.
Comments
Post a Comment