Java: Switch Statement

The switch statement is quite similar to an if statement. However, the former does not work with a range of values, and instead, each case can engage with just one value. Based on the value that has been used for switching, the appropriate code is executed. 

A switch statement follows this syntax:

switch (variable used for switching) {
    case aCase: 
        do A;
    case bCase: 
        do B;
        do C;

Using a switch statement enables coders to have as many cases as the program may require. In the switch statement, using a default case is not mandatory as it only gets executed if none of the other cases is applicable.  A byte, short, char, or int variable can be used for switching, although Java 7 allows even string variables to be used. 

If a certain case is valid, each subsequent line is executed until the compiler encounters a break statement. A break statement instructs the compiler to move out of the switch statement to the rest of the program.  

Let us look more closely at this statement through an example. Launch NetBeans and create a new project called ExampleSwitch. Substitute the existing code with the following code. This example uses a String variable: 

import java.util.Scanner;

public class ExampleSwitch { 
    public static void main(String[] args) { 
        Scanner input = new Scanner(; 
        System.out.print("Please type the grade: ");
        String gradeExample = input.nextLine().toUpperCase(); 
        switch (gradeExample)
            case "A+":
            case "A":
            case "B":
            case "C":

In this program, the grade of the user is sought, after which the next line reads the input, which is stored in gradeExample using the following statement:

String gradeExample = input.nextLine().toUpperCase();

Don’t be confused by this statement; it is not very complex. Both the methods are being called in a single statement.   

input.nextLine(), is used to read the user’s input, and the returned variable will be of string type. Then, the inputted String is converted to upper case with the toUpperCase() method. This statement is a good example of how two methods can be called in the same statement. The left method is first executed and then the right, which is the nextLine() method and the toUpperCase()method, respectively.

Because Java is case sensitive, user input must be converted to upper case before assigning it to gradeExample. Both ‘a’ and ‘A’ must return ‘Outstanding” and hence, the input is converted to uppercase before being assigned to gradeExample.

Once the user’s age is obtained, Switch statements dictate the output. 

In case the grade that is inputted is “A+”, every subsequent line is executed until the break statement is encountered. Therefore, the result that is printed is “Outstanding”.

In case the grade entered is “A” (Line 16), both Line 17 and 18 are executed. The resultant output is again “Outstanding”.

In case the grade that is entered is neither “A+” nor “A”, the next case is assessed. Every case is checked from top to bottom until a case is satisfied. If no cases apply, the default case is executed.

At this point, various inputs such as A, B, and C can be entered to see the following outputs:

Enter the grade: A+ 
Enter the grade: A 
Enter the grade: B 
Enter the grade: C 
Enter the grade: D 
Enter the grade: Hello 

Leave a Reply

%d bloggers like this: