Java Switch, Case, Default and Break Statements

Java Switch Statement

In programming, decisions can be one, two, or multi-branched. Java's switch statement is the most suitable construct for multi-branched decisions. An if statement causes a branch in the flow of a program's execution. You can use multiple if statements, as shown in the previous section, to perform a multiway branch. This is not always the best solution, however, especially when all of the branches depend on the value of a single variable. In this case, it is inefficient to repeatedly check the value of the same variable in multiple if statements.

A better solution is to use a switch statement. A switch statement starts with an expression whose type is an int, short, char, or byte. Here is the syntax of a switch statement:

switch (int-or-char-value) 
{
    case label_1:
    // statement sequence
    break;
    case label_2:
    // statement sequence
    break;
    .
    .
    .
    case label_N:
    // statement sequence
    break;
    default:
    // default statement sequence
}

The default clause is optional in a switch construct. Hence, you can omit default if this is not required. Execution in a switch, starts from the the case label that matches the value provided in switch's parentheses and continues until the next break is encountered or the end of the switch. If none of the case labels match, then the default clause is executed, if it is present.

Note that default need not be the very last delegate of statements, it can be placed anywhere in the body of switch. But, while doing so never forget to place a break after default block of statements. If the default is the last thing to do in switch's body then of course, no break is needed because the body of switch gets ended right after the default.

Let's implement determining if an alphabet is vowel or consonant by the switch construct.

//Demonstrates switch-case and break
public class ControlFlowDemo
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter an alphabet (a-z or A- Z): ");
        char ch = in.next().charAt(0);
 
        switch (ch)
        {
            default:
                System.out.println(ch + " is consonant."); break;
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                System.out.println(ch + " is vowel.");
        }
    }
}
 
OUTPUT
======
Enter an alphabet (a-z or A- Z): A
A is vowel.

If you look at above program carefully, you will observe that it takes input from the user. We would input one character long string and then assign its first character to variable ch. This ch then passed to switch to check whether it is a vowel or consonant. Inside switch block default is placed first to demonstrate that default can be placed anywhere in the switch block, provided that break is used appropriately after default section.

Java Break Statement

Java's control flow statements define two jump statements to jump from one place to another by defying the natural flow of control. These statements are break and continue. Java also has reserved goto keyword, but this is not currently used. As you have seen in above example, break stops the execution at the place it is executed and gets control out of the block. In above example, break has been used in conjunction with switch statement, but it is also used to abruptly terminate a do or for or while loop.

Last Word

In this tutorial we discussed Java's switch, case, default and break statements. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.