Switch Case Statement In Java Tutorial with Examples – POFTUT

Switch Case Statement In Java Tutorial with Examples

Switch ... Case statement is a popular selection statement used in Java programming. Switch-case simply set cases and related code block to be executed when given case match.

Syntax

The complete syntax of the switch case statement in Java is like below.

  • switch is the keyword used to create a new switch-case block.
  • EXPRESSION is the expression which will be checked against all cases like CASE1, CASE2 … except for the default case.If it is matched the related case block will be executed.
  • case CASE1 defines a case and provide the case code block to be executed if the case is matched with the EXPRESSION.
  • CASE1_CODE_BLOCK will be executed if the CASE1 is matched with the EXPRESSION.
  • break; statement will end the current case and the whole case-switch which means the following cases will not be checked. break; statement is optional but it is generally used as a good practice.
  • default is the default case if there is no match the default case will be executed with the DEFAULT_CODE_BLOCK
  • DEFAULT_CODE_BLOCK is executed when the default case matched.

Switch Case Example

We will create an example we will print the weekday name according to the given day number.

Switch Case Example

Switch Case Example

We can see that the variable named day stores the day number and checked against other day numbers from 1 to 7. The day number matches the case 5 which is Friday and it will be printed to the screen. Thebreakstatement will end the current switch-case.
<h1>Switch Case String Example</h1>
We can also use string expression in order to match the given cases. We will check in the reverse form of the previous example. We will provide the
Fridayday name and print the day number to the console. If we are using String for math under the hood the String typeequals()` method will be used by the switch-case.

LEARN MORE  Switch Case Statement in C/C++ Tutorial with Examples

Switch Case String Example

Switch Case String Example

This code will print 5 to the console because the “Friday”  is the 5th day of the week. We will set the day_name variable as “Friday” as a string and check with the switch-case.

Switch Case Enum Example

We can also use enum type data for switch-case. We will create an enum type Day and create a new variable with the Day enum type named Now. Then we will set Day.Friday to the Now.

Switch Case Enum Example

Switch Case Enum Example

Switch Case Rules

While using switch-case there are some rules to obey and work the switch-case properly.

  • The duplicate case is not allowed which will conflict and give an error.
  • The expression and case values should be the same type in order to check, compare and match properly.
  • Case values should be constant or literal (String or Number). Variables are not allowed.
  • The break statement is optional where it can be omitted for the whole switch-case.
  • When the break statement is reached the whole switch-case will end.
LEARN MORE  Linux Bash Case Statement with Examples

Break Keyword

We have used all of our previous examples the break keyword.  The break keyword is used to stop the switch-case and then exit from the switch-case which will prevent to check the following cases. This can be useful because after a single match we may do not need the next checks. If we forget the break statement the underneath code blocks will be executed.

Default Keyword/Case

The default case of the keyword is used when there is no match for the given cases and we want to run some code. The default keyword also named default case which simply means run default case where there is no match. This can be especially useful to run a default code. We can put the default case anywhere in the switch-case but in order to make the code more readable putting default to the end of the switch-case is a best practice.

Default Keyword/Case

Default Keyword/Case

In the example, the day is 50 and this will not match in the given cases so the default case will be run which will print to the screen “Please provide proper day number”.

LEARN MORE  Linux Bash Case Statement with Examples

Nested Switch Case

switch-case can be nested in multiple times. But keep in mind that this will make the application mess and harder to read other programmers. In the following example, we will add a nested switch-case to the “Sunday” case.

Nested Switch Case

Nested Switch Case

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)