Java switch case statement with demo

The switch case statement

In the real World, where there are more than one options we have to decide. One of the methods that we use in the Java programming is the switch case statement – to take the decisions based on different situations.

The decision-making statements in Java

Two types of decision-making statements provided by Java are:

  1. The If statement
  2. The Switch statement
A switch-case example

This chapter will explain the switch case statement only. For the if statement go to java if statement chapter.

The switch case in java

The switch case statement is used when you have many options and you have to execute one of those. The Java switch statement is useful when you have long blocks of the if, else if code.

In the switch case statement, a variable is tested against a list of values. Where each value is called a case. Following is an example to explain how the switch case statement works, first look at its syntax.

Syntax of the Java switch statement

The general syntax of using the switch case statement is:

switch (condition) {

   case value :

    // Code to be executed if condition is true

    brerak; //This is optional, The execution will stop here for switch statement

   case value :

    // Code to be executed if condition is true

    brerak; //This is optional, The execution will stop here for switch statement

   case value :

    // Code to be executed if condition is true

    break; //This is optional, The execution will stop here for switch statement

default:  // This is optional and works like else in if statement

    // Code to be executed if condition is true

}

Main points about Switch-Case statement

A few points to be noted while using the switch with case statement:

  • The data type of the variables used in the switch statement can be the byte, char, short and int.
  • The value used in the java case statement must be same data type as in the switch.
  • The break statement is used to terminate the execution within the switch block or it will keep on executing the other cases.
  • The default block is used, which is optional. In a situation where none of the Java case statement value is matched, the default part will be executed and after that execution will move to the next statement outside of Java switch block.
  • You can use any number of cases in the switch java statement.

Example of using switch case statement

The following example uses the Java switch statement where a variable daynumber is used. If the daynumber value is 1 then it will display Mon, 2 for Tue and so on.


Experience this online

The output of the above code will be:

Wed

In the example, you can see the variable daynumber is used in the switch statement. A value is assigned to that variable that you can assign dynamically, depending on the scenario. After that, five cases are used with different values to check with the break statement.

 

Also see – The for loop | While loop of Java

Was this article helpful?

Related Articles

Leave A Comment?