Video Lecture – 1?

1 - Introduction and Installing the java (JDK) Step by Step Tutorial 2 - Installing Eclipse IDE and Setting up Eclipse 3 - Creating First Java Project in Eclipse IDE

Video Lecture – 2?

4 - Variables and Types in Java 5 - Getting User Input using Java

Video Lecture – 3?

6 - Math and Arithmetic Operators in Java 7 - Increment Operator and Assignment Operator 8 - IF ... ELSE Statements and Relational Operators 9 - Logical Operators in Java

Video Lecture – 4?

10 - switch Statement in Java 11 - The while Statements (while Loops) 12 - The do-while Statements (do-while Loops)

Video Lecture – 5?

13 - Arrays in Java 14 - The for Statement in Java (for loops) 15 - Java String 16 - Introduction to Methods

Video Lecture – 6?

17 - Parameter passing and Returning a Value from a Method 18 - Classes and Objects in Java 19 - Class Constructor in Java 20 - Method Overloading in Java

Video Lecture – 7?

21 - 'static' keyword in Java 22 - 'static' keyword Example in Java 23 - Public, Private, Protected and this (Java Access Modifiers) 24 - The final keyword in Java

Video Lecture – 8?

25 - Inheritance in Java 26 - Polymorphism in Java 27 - Method Overriding in Java 28 - Abstract Methods and Classes

Video Lecture – 9?

29 - Java Interfaces 30 - Recursion in java 31 - Arraylist in Java 32 - LinkedList in Java 33 - Difference between LinkedList vs ArrayList in Java

Video Lecture – 10?

34 - ListIterator in Java 35 - HashSet in Java 36 - Catching and Handling Exceptions in Java using Try Catch Blocks 37 - Java Finally block (try-catch-finally Exception Handling in Java )

Video Lecture – 11?

38 - Create a File and Write in it Using PrintWriter and File class 39 - How to Read file using Java 40 - Using Date & Time + formatting Date using SimpleDateFormat

Revision

Java Flow Control

Java Arrays

Java OOP (I)

Java OOP (II)

Java OOP (III)

Java Exception Handling

Java List

Java Queue

Java Map

Java Set

Java I/O Streams

Java Reader/Writer

Additional Topics

Java switch Statement

In this tutorial, you will learn to use the switch statement in Java to control the flow of your program’s execution with the help of examples.

The switch statement allows us to execute a block of code among many alternatives.

The syntax of the switch statement in Java is:

switch (expression) {

  case value1:
    // code
    break;
  
  case value2:
    // code
    break;
  
  ...
  ...
  
  default:
    // default statements
  }

How does the switch-case statement work?

The expression is evaluated once and compared with the values of each case.

  • If expression matches with value1, the code of case value1 are executed. Similarly, the code of case value2 is executed if expression matches with value2.
  • If there is no match, the code of the default case is executed.

Note: The working of the switch-case statement is similar to the Java if…else…if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.


Example: Java switch Statement

// Java Program to check the size
// using the switch...case statement
class Main {
  public static void main(String[] args) {
    int number = 44;
    String size;
    // switch statement to check size
    switch (number) {
      case 29:
        size = "Small";
        break;
      case 42:
        size = "Medium";
        break;
      // match the value of week
      case 44:
        size = "Large";
        break;
      case 48:
        size = "Extra Large";
        break;
      
      default:
        size = "Unknown";
        break;
    }
    System.out.println("Size: " + size);
  }
}

Output:

Size: Large

Let’s try the above code :

In the above example, we have used the switch statement to find the size. Here, we have a variable number. The variable is compared with the value of each case statement.

Since the value matches with 44, the code of case 44 is executed.

size = "Large";
break;

Here, the size variable is assigned with the value Large.

Recommended Reading: Create a Simple Calculator Using the Java switch Statement


Flowchart of switch Statement

Flowchart of the Java switch statement
Flow chart of the Java switch statement

break statement in Java switch…case

Notice that we have been using break in each case block.

 ...
case 29:
  size = "Small";
  break;
...

The break statement is used to terminate the switch-case statement. If break is not used, all the cases after the matching case are also executed. For example,

class Main {
  public static void main(String[] args) {
    int expression = 2;
    // switch statement to check size
    switch (expression) {
      case 1:
        System.out.println("Case 1");
        // matching case
      case 2:
        System.out.println("Case 2");
      case 3:
        System.out.println("Case 3");
      default:
        System.out.println("Default case");
    }
  }
}

Output

Case 2
Case 3      
Default case

Let’s try the above code :

In the above example, expression matches with case 2. Here, we haven’t used the break statement after each case.

Hence, all the cases after case 2 are also executed.

This is why the break statement is needed to terminate the switch-case statement after the matching case. To learn more, visit Java break Statement.


default case in Java switch-case

The switch statement also includes an optional default case. It is executed when the expression doesn’t match any of the cases. For example,

class Main {
  public static void main(String[] args) {
  
    int expression = 9;
    
    switch(expression) {
        
      case 2:
        System.out.println("Small Size");
        break;
      case 3:
        System.out.println("Large Size");
        break;
            
      // default case
      default:
        System.out.println("Unknown Size");
    }
  }
}

Output

Unknown Size

In the above example, we have created a switch-case statement. Here, the value of expression doesn’t match with any of the cases.

Hence, the code inside the default case is executed.

default:
  System.out.println("Unknown Size);

Let’s try the above code :


Note: The Java switch statement only works with:

  • Primitive data types: byte, short, char, and int
  • Enumerated types
  • String Class
  • Wrapper Classes: Character, Byte, Short, and Integer.