JAVA Switch Statement

As we already know that statements in JAVA program are executed sequentially from top to bottom, but we can control the flow of program execution by using control statements. We are already familiar with if-else statement in our previous post. In case of if-else statement a statement or group of statements are executed if some condition is satisfied.

switch statement is similar to the if-else in working but it is implement in such a situation where we have to execute a particular statement or group of statement from number of possibilities. Same logic can also be implemented with if-else statement but it become cumbersome.

For example consider the below programming logic implemented with if-else statement.


    if (grade=='A')
   System.out.println(“You got an A. excellent job!”);
    else if (grade=='B')
    System.out.println(“You got an B. Good job!”);
    else if (grade=='C')
    System.out.println(“You got an C. Satisfactory job!”);
    else if (grade=='F')
    System.out.println(“You got an F. Poor job!”);





if above if-else statement gives output depending the the value of grade but the situation become co-cumbersome and tedious. Same can be achieved easily with JAVA Switch statement. like this


    switch (grade) {
    case ‘A’:
    System.out.println(“You got an A. Excellent job!”);
    break;
    case ‘B’:
    System.out.println(“You got a B. Good job!”);
    break;
    case ‘C’:
    System.out.println(“You got a C. Satisfactory Job”);
    break;
    default:
    System.out.println(“You got an F. Poor job!”);
    }


The first line of the switch statement specifies the variable that is tested—in this example, grade. Then, the switch statement uses the { and } brackets to form a block statement.Each case statement checks the test variable in the switch statement against a specific value. The value used in a case statement can be a character,an integer, or a string. In the preceding example,there are case statements for the characters A, B, and C. Each has one or two statements that follow it. When one of these case statements matches the variable inswitch, the computer handles the statements after the case statement until it encounters a break statement.

For example, if the grade variable has the value of B, the text “You got a B. Good job!” is displayed. The next statement is break, so nothing else in
the switch statement is executed. The break statement tells the computer to break out of the switch statement.
The default statement is used as a catch-all if none of the preceding case statements is true. In this example, it occurs if the grade variable does not equal A, B, or C. You do not have to use a default statement with every switch block statement you use in your programs. If it is omitted, nothing
happens if none of the case statements has the correct value.

Java 7 and above adds support for using strings as the test variable in a switch-case statement. The Commodity class in below program Listing uses this statement to either buy or sell an unspecified commodity. The commodity costs $20 when purchased and earns $15 when sold.

A switch-case statement tests the value of a string named command, running one block if it equals “BUY” and another if it equals “SELL”.

    LISTING  The Commodity Program

    1:     public class Commodity {
    2:     public static void main(String arguments) {
    3:     String command = “BUY”;
    4:     int balance = 550;
    5:     int quantity = 42;
    6:
    7:     switch (command) {
    8:     case “BUY”:
    9:     quantity += 5;
    10:     balance -= 20;
    11:     break;
    12:     case “SELL”:
    13:     quantity -= 5;
    14:     balance += 15;
    15:     }
    16:     System.out.println(“Balance: “ + balance + “\n”
    17:     + “Quantity: “ + quantity);
    18:     }
    19:     }

This application sets the command string to “BUY” in line 3. When the switch is tested, the case block in lines 9–11 is run. The quantity of the commodity increases by 5 and the balance is lowered by $20.

The main difference between unlike if/else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. JAVA 7 and above also supports strings data type.

Difference Between if-else and Switch Statement.


Main difference between if-else and switch statement is that the statement(s) inside the if are executed depending on the evaluation of boolean expression as true or false.
On the other hand switch satement chooses values of the variable inside the switch and executes the statement on matching the values with the case.