Select Page

How to use the switch statement in Java

The switch statement is a multi-way decision statement. It provides an easy way to change the path of execution to different parts of the program based on the value of the expression.

nanadwumor

May 27, 2023

The expression passed to the switch statement can be a byte, short, char, int , enum data type.

Beginning with JDK7, switch was allowed to accept variables of  String data types.

Hence, a switch statement accepts arguments of types : byte, short, int, char, enum, String.

Syntax of the switch statement


Join other Subscribers on our YouTube channel and enjoy daily pogramming tutorials.


How does the switch statement work?

The first line of the statement starts with the word switch which is followed by a testExpression. The testExpression is enclosed  in parentheses.

The testExpression is of the data types char, byte, short, int, or string.

After the switch textExpression, a curly bracket opens to enclose all the internal code of the switch.

Inside the curly brackets are cases of the switch statement.

A case is simply one of the options the switch statement will select per the value of the testExpression.

A case section begins with the keyword case, followed by a value and a colon. This value will be tested against the testExpression and if they’re equal, the switch statement branches and gives control to that part of the code.

For each case, there are one or more statements which are followed by a break statement.

The break keyword ensures that after all statements under one case section are executed, control doesn’t continue to the next succeeding case. It literally “breaks” that path of execution.

 After all of the case sections, the last section is an optional default section. This is the default section that will only execute if a case is not found to match the testExpression

Also, the default statement will get executed even if a case is found in situations where proper break statements are not inserted.

Recommended



Program to demonstrate use of switch statement to print months of the year

Output

Program to demonstrate use of switch statement to print out months of the year

Explanation of code

If the value in the month variable is 1, the program will branch to the case 1 section and execute the statements under the case 1.  

That’s, the statement System.out.print( “January”) will get executed.

If the value in the month variable is 2, the program will branch to the case 2 section and executes the statements at that case section.

That’s, the statement System.out.print(“February”) gets executed.

The break statement that comes after the statement causes the program to break. This causes the program to exit the switch statement.

If the value in the month variable is 3, the program will branch to the case 3 section and executes the statements at that case section.

That’s, the statement System.out.print(“March”) gets executed.

The break statement that comes after the statement causes the program to break. This causes the program to exit the switch statement.

In the same order, case 4 will print April; case 5 will print May with case 12 printing December.

If any number aside 1 to 12 is passed to the month variable, no case is selected so the program goes to the default statement and execute the statements there.

Flowchart of the switch statement

flowchart of switch control statement in Java

What rules govern the use of the switch statement?

  1.  Duplicate cases are not allowed. That is, you cannot have the same case more than once in a switch statement.                                 Example, the following code is a section of the program above. It takes an int value and prints out the corresponding month of the int value. The code has two cases with the value 2. This is wrong.

2. The value of the switch expression should be of the same data type as the value of the switch case.  That is, if switch accepts int as textExpression, then the cases must also be int. In the same vein, if the texEpression is a String, the cases must have String values.

  1. The value of a switch case must be a constant and not a variable. If a variable is used, it should be initialized to a constant literal.

In this example, the switch expression is a variable initialized to a String literal.

  1. The break statement is optional. It is used inside the switch to terminate a statement sequence. If omitted, execution will continue on into the next case. In the below switch statement, case ‘a’ and case ‘A’ do not have a break statement. This means, if the switch textExpression is ‘a’ , it will fall through to case ‘A’. The same is true for the other cases.
  1. The default statement is optional. The default statement is executed if none of the cases is selected and executed.

The switch statement and the if-else-if statement

The switch statement provides a relatively better alternative to a large series of if-else-if statement. The if-else-if statement becomes more complex if the options are many.

if-else-if program to calculate grade of student

Equivalent switch program to calculate grade of student

switch statement with no breaks

In a switch statement, 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. For example, consider the following program:

Output

swtich statement in Java with no breaks

The default section is optional

Unless you want a default statement to execute if no case is selected, the default statement is optional and can be omitted just like in this example.

The break statement at the default section is optional

When the program gets to the default section, it executes the statements under that section and comes out of the switch statement even if there is no break statement. 

This is because the default section is the last section. You may decide to put a break there or not. The program will still halt after execution.


Share this article


You May Also Like…

double data type in Java

double data type in Java

The double data type is also a floating point number. It is called double because it has a double-precision decimal...

float data type in Java

float data type in Java

It is a floating point number. It has single precision data type. It stores a floating point number of 7 digits of...