Limited access

Upgrade to access all content for this subject

A switch statement is a programming construct for comparing a variable against a set of possible values, referred to as cases. When a matching case is found, code execution begins at that case and continues until program flow is interrupted (for example, by a break or return statement). Switch statements can use various primitive data types, along with their respective wrapper classes, as input.

You are provided with the following function, which includes a switch statement:

public int calculate(String op, int a, int b) {
    int result;
    switch(op) {
        case "ADD":
            result = a + b;
        case "REVERSE SUBTRACT":
            int temp = a;
            a = b;
            b = temp;
        case "SUBTRACT":
            result = a - b;
        case "SQUARE":
            b = a;
        case "MULTIPLY":
            result = a * b;
        case "DIVIDE":
            result = a / b;
            result = 0;

    return result;
Select an assignment template