• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Switch Statement

A switch statement matches against multiple expressions and transfers control to a statement based on the given expression. Optionally a default case can be included which will be executed if no match is found.

switch(test-expression) {
    case expression-1:
        statements;
    case expression-2:
        statements;
    .
    .
    case expression-n:
        statements;
    default:
        statements;
}

Once it matches against the test expression, it will fall through the remaining cases till the end of switch including default. To prevent this fall through of one case to other case, we need use break keyword to exit out of the switch statement.

Below program, matches given operator with multiple operators, jumps directly to multiplication case and exits the case after executing it.

package com.techstackjournal;

public class SwitchExample1 {

	public static void main(String[] args) {
		int num1 = 10, num2 = 2;
		char operation = '*';

		switch (operation) {
		case '+':
			System.out.println(num1 + num2);
			break;
		case '-':
			System.out.println(num1 - num2);
			break;
		case '*':
			System.out.println(num1 * num2);
			break;
		case '/':
			System.out.println(num1 / num2);
			break;
		case '%':
			System.out.println(num1 % num2);
			break;
		default:
			System.out.println("Invalid operation");

		}

	}

}
20

In the test expression, you can include integer, char and String. We’ll learn about what is String later.

Sometimes, you may want to fall through other cases. In that case, you can remove break between cases. In the below program we want to match a character with vowels, if no match found it will run default case.

package com.techstackjournal;

public class SwitchExample2 {

	public static void main(String[] args) {
		char letter = 'e';

		switch (letter) {
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
			System.out.println("It's a vowel");
			break;
		default:
			System.out.println("It's a consonant");

		}

	}

}
It's a vowel

Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

  • Java Tutorial
    • Introduction
    • Install Java
    • First Java Program
    • Statements and Comments
    • Packaging the Classes
    • Variables
    • Primitive Data Types
    • Operators
    • if-else statement
    • Loops in Java
    • Arrays
    • break and continue
    • Switch Statement
    • Classes and Objects
    • Methods
    • Encapsulation
    • Constructor in Java: All You Need To Know
    • Inheritance
    • Access Modifiers
    • Static Variables and Methods
    • Final keyword in Java
    • Abstract Class
    • What is an Interface in Java?
    • Method Overloading
    • Java – Method Overriding
    • Polymorphism
    • Java – super Keyword
    • Nested Classes
    • Exception Handling
    • String Class

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in