Let’s learn switch statement in java.
Switch statement in java
Switch statement is used to choose one of the few blocks of code to get some output. Here in switch statement switch expression is evaluated once and compared with each “case” value.
Then if there is a match with any of the “case”, corresponding code block is executed.

The value of case should be a literal or constant. If the variable being switched is equal to case then statements corresponding to that case will execute until break statement.
Switch terminates when it is reached break statement. Then the control shifts to next switch statement.
It’s not mandatory to add break statement in every case. If there’s no break statement then flow of control will continue to next subsequent case until break statement.
It is a good programming practice to have a default case in switch statement which should appear at the end. default case can be used to perform a task when none of the cases is true.
Datatypes allowed in switch statement are byte, short, char and int until Java 1.4 version. But from Java 1.5 version onwards corresponding wrapper classes and enum type also allowed. From Java 1.7 version onwards String type also allowed.
We can have any number of case statements in switch expression. Value for “case” must be same datatype as variable in switch.
Syntax for switch
switch(expression) { case 1 : // code break; case 2 : // code break; case 3 : // code break; . . . . . . case n : // code break default: // default code }
Switch statement is comprised of switch keyword. The value that we want to test the expression. Then starts a code block and then type the word case.
We then type in the code that we want to execute. Then we type in break keyword. What Break keyword does is it terminates the enclosing switch statement and control continues at the line after the switch block.
In the above syntax default means any other case that is not been covered above.
Valid switch statement: switch(5 + 6 + 56) switch(5 * 6 + 7 % 8)
Invalid switch statement: switch(pq + rs) switch(p + q + r)
Example for switch statement in java
Here’s switch statement in java program.
public class SwitchStatementExample { public static void main(String[] args) { char grade = 'A'; switch(grade) { case 'A' : System.out.println("Distinction."); break; case 'B' : case 'C' : System.out.println("First class."); break; case 'D' : System.out.println("You have passed."); case 'F' : System.out.println("Fail. Try again."); break; default : System.out.println("Invalid grade"); } System.out.println("Grade is: " + grade); } }
Output:
Distinction.
Grade is: A
Grade program using switch case
Now let’s see grade program using switch statement in java. Here we have to compute marks based on given grades.
So if grade is A then marks will be greater than or equal to 80, if grade is B then marks will be greater than or equal to 60 and less than 80.
If grade is C then marks will be greater than or equal to 40 and less than 60. Lastly if grade is F then marks will be less than or equal to 40.
Print invalid grade if any other grade is typed by user. Here’s the grade program using switch statement.
import java.util.Scanner; public class GradeSwitch { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter grade from A, B, C or F: "); String strInput = sc.next(); char grade = strInput.charAt(0); switch (grade) { case 'A': System.out.println("Grade A - distinction"); break; case 'B': System.out.println("Grade B - first class"); break; case 'C': System.out.println("Grade C - second class"); break; case 'F': System.out.println("Grade F - fail"); break; default: System.out.println("invalid grade"); break; } sc.close(); } }
Output:
Please enter grade from A, B, C or F): A
Grade A – distinction
Please enter grade from A, B, C or F): E
invalid grade
Also read – this keyword in java