Java Switch Statement Char Example

Java Switch Case Statement with Examples

❮ Previous Chapter Next Chapter ❯


In this quick article, we will look into Switch case statement, which is used when we have a number of options (or choices) and we may need to perform a different task for each choice.

Table of contents

  1. Switch Case Simple Example
  2. Switch Case Statement with Break
  3. Java Switch Statement with String
  4. Java Switch Statement with Enum
  5. Nested switch Statements

The switch statement is Java's multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements.

Syntax

Here is the general form of a switch statement:

                switch                (expression) {                case                value1:                                  //                  statement sequence                break;                case                value2:                                  //                  statement sequence                break;                .                .                .                case                valueN:                                  //                  statement sequence                break;                default                :                                  //                  default statement sequence                }

For versions of Java prior to JDK 7, an expression must be of type byte, short, int, char, or an enumeration. Beginning with JDK 7, an expression can also be of type String. Each value specified in the case statements must be a unique constant expression (such as a literal value). Duplicate case values are not allowed. The type of each value must be compatible with the type of expression.

Switch Case Flow Diagram

The switch statement works like this: The value of the expression is compared with each of the values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants match the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.

1. Java switch case Simple Example

The following code example, SwitchDemo, declares an int named month whose value represents a month. The code displays the name of the month, based on the value of the month, using the switch statement.

                public                class                SwitchDemo                {                public                static                void                main(String[]                args) {                int                month                =                8;                String                monthString;                switch                (month) {                case                1                :                monthString                =                                  "January"                ;                break;                case                2                :                monthString                =                                  "February"                ;                break;                case                3                :                monthString                =                                  "March"                ;                break;                case                4                :                monthString                =                                  "April"                ;                break;                case                5                :                monthString                =                                  "May"                ;                break;                case                6                :                monthString                =                                  "June"                ;                break;                case                7                :                monthString                =                                  "July"                ;                break;                case                8                :                monthString                =                                  "August"                ;                break;                case                9                :                monthString                =                                  "September"                ;                break;                case                10                :                monthString                =                                  "October"                ;                break;                case                11                :                monthString                =                                  "November"                ;                break;                case                12                :                monthString                =                                  "December"                ;                break;                default                :                monthString                =                                  "Invalid month"                ;                break;         }                System                .out.println(monthString);     } }

In this case, August is printed to standard output.

2. Java switch Statement with Break (break statements are optional)

The break statement is optional. If you omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them.

                package                net.javaguides.corejava.controlstatements.switchcluase;                public                class                MissingBreak                {                public                static                void                main(String                args[]) {                for                (int                i                =                0; i                <                12; i++)                switch                (i) {                case                0                :                case                1                :                case                2                :                case                3                :                case                4                :                System                .out.println(                  "i is less than 5"                );                break;                case                5                :                case                6                :                case                7                :                case                8                :                case                9                :                System                .out.println(                  "i is less than 10"                );                break;                default                :                System                .out.println(                  "i is 10 or more"                );             }     } }

Output:

              i is less than 5 i is less than 5 i is less than 5 i is less than 5 i is less than 5 i is less than 10 i is less than 10 i is less than 10 i is less than 10 i is less than 10 i is 10 or more i is 10 or more                          

As you can see, execution falls through each case until a break statement (or the end of the switch) is reached.

3. Java switch Statement with String

Beginning with JDK 7, we can use a string to control a switch statement.

The below example demonstrates the use of a string to control a switch statement.

                package                net.javaguides.corejava.controlstatements.switchcluase;                public                class                StringsSwitch                {                public                static                void                main(String                args[]) {                String                str                =                                  "two"                ;                switch                (str) {                case                                  "one"                                :                System                .out.println(                  "one"                );                break;                case                                  "two"                                :                System                .out.println(                  "two"                );                break;                case                                  "three"                                :                System                .out.println(                  "three"                );                break;                default                :                System                .out.println(                  "no match"                );                break;         }     } }

Output:

              two            

4. Java switch Statement with Enum

In real-time projects, enums are frequently used with a switch-case statements.

For example:

                package                net.javaguides.corejava.controlstatements.switchcluase;                public                class                EnumInSwitchStatement                {                public                static                void                main(String[]                args) {                System                .out.println(enumInSwitch(Days                                  .SUNDAY));     }                enum                Days                {                SUNDAY,                MONDAY,                TUESDAY,                WEDNESDAY,                THURSDAY,                FRIDAY,                SATURDAY;     }                public                static                String                enumInSwitch(Days                day) {                switch                (day) {                case                SUNDAY                :                return                                  "Its Sunday!!"                ;                case                MONDAY                :                return                                  "Its Monday"                ;                case                TUESDAY                :                return                                  "Its Tuesday"                ;                case                WEDNESDAY                :                return                                  "Its Wednesday"                ;                default                :                return                                  "Rest of the week...."                ;         }     } }

Output:

              Its Sunday!!            

5. Java Nested switch Statements

We can use a switch as part of the statement sequence of an outer switch. This is called a nested switch.

For example, the following fragment is perfectly valid:

                switch                (count) {                case                1                :                switch                (target) {                                  //                  nested switch                case                0                :                System                .out.println(                  "target is zero"                );                break;                case                1                :                                  //                  no conflicts with outer switch                System                .out.println(                  "target is one"                );                break;         }                break;                case                2                :                                  //                  ...              

Here,case 1: statement in the inner switch does not conflict with the case 1: statement in the outer switch. The count variable is compared only with the list of cases at the outer level. If a count is 1, then the target is compared with the inner list cases.


❮ Previous Chapter Next Chapter ❯

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

wallsenisho.blogspot.com

Source: https://www.javaguides.net/2018/10/java-switch-case-statement-with-examples.html

0 Response to "Java Switch Statement Char Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel