Wednesday, February 1, 2017

Java’s Switch Statement in Three Minutes

Java's switch statement allows the comfortable selection of one of many execution paths based on a variable's value. The variable must either be an enum, a String, or an integral type like int. Given to switch, it is compared with a list of case labels that each specify a value - as soon as the first one matches, the corresponding statement block is executed. The switch statement works much like a long if-else-if chain and can often be used to replace it.

This article only requires working knowledge of integers and strings but the more you know about if and particularly if-else-if the more you will get out of it. Experience with other numeric types, enums and methods are a bonus.

Using switch

Let's jump right in and start with an example! The following switch statement writes a textual representation of the first three natural numbers to the console:

// any number is fine
int number = 2;
switch (number) {
    case 0:
        System.out.println("zero");
        break;
    case 1:
        System.out.println("one");
        break;
    case 2:
        System.out.println("two");
        break;
    default:
        System.out.println("many");
        break;
}

Just from looking at it, what do you think will happen?

The switch will look at number, which is currently 2, and compare it with each of the values behind the case keywords. It's not 0, it's not 1, it's 2. Bingo! So off it goes calling System.out.println("two").

Syntax of switch

You can use the switch statement with variables of type int, byte, short, char (note that long does not work), String, or an enum (or enumeration type as they are formally called). Here's how it works:

switch (<variable>) {
    case <value>:
        // statements
        break;
    case <other-value>:
        // statements
        break;
    case <more-values>:
        // statements
        break;
    default:
        // statements
        break;
}

You use the keyword switch followed by the variable you want to switch over (as it is commonly phrased) and a pair of curly braces. Inside those curly braces, you list as many branches as you like.

Each regular branch consists of the keyword case, a value that matches the variable's type (meaning it could be assigned to it), and a colon. Together, these three things are called a switch label. It is followed by the statements you want to execute if the variable has that particular value. Unless you have a very good reason, every switch-branch should end in a break. (I'll explain in a minute, why.)

If you need a branch that is executed if none of the labels matched, you can create one with default. It works much like a regular branch but takes no value.

Fall-Through

Continue reading %Java’s Switch Statement in Three Minutes%


by Nicolai Parlog via SitePoint

1 comment:

  1. Nice article on switch statement. Explained very well. Thanks.

    Cheers,
    http://www.flowerbrackets.com/switch-statement-java/

    ReplyDelete