- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
switch statement in Java with examples
The "switch" statement lets developers write code that reacts differently depending on an expression's value.
In other words, we can say that a "switch" statement in Java is a control flow clause that enables you to carry out various operations depending on the value of a variable or expression. It offers a substitute for using numerous if-else statements.
Java switch statement syntax
The following is the general form of the "switch" statement in Java.
switch (expression) { case value1: // code to execute if "expression" evaluates to "value1" break; case value2: // code to execute if "expression" evaluates to "value2" break; case value3: // code to execute if "expression" evaluates to "value3" break; : default: // code to execute if "expression" does not match any of the values }
Each "case" label is compared to the "expression" after it has been evaluated once. If the expression matches a case label, the code block immediately following that label is executed until the "break" statement is reached, at which point the switch statement is terminated. If there is no match, the code block following the "default" label (if it exists) is executed.
Java switch statement example
Consider the following Java code as an example demonstrating the "switch" statement:
public class JavaProgram { public static void main(String args[]) { int i; for (i = 0; i < 10; i++) { switch (i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break; case 3: System.out.println("i is three."); break; case 4: System.out.println("i is four."); break; case 5: System.out.println("i is five."); break; default: System.out.println("i is greater than 5."); } } } }
i is zero. i is one. i is two. i is three. i is four. i is five. i is greater than 5. i is greater than 5. i is greater than 5. i is greater than 5.
This Java program employs a "switch" statement to determine the value of the variable "i" and run various blocks of code in accordance with that value. Java's "switch" control flow statement enables the program to run various code blocks depending on the result of an expression.
The expression being evaluated in this program is the variable "i." There are several cases in the "switch" statement, each of which corresponds to a different value of "i." If the value of "i" corresponds to a case, the code contained within that case is executed. If none of the cases correspond to the value of "i" the "default" case is executed.
When the value of "i" is zero, one, two, three, four, or five. The code block associated with these case values is run. In contrast, the code associated with "default" is executed in other values of "i."
As you can see, each time through the loop, the statements associated with the case constant that matches i are executed. All the others are bypassed. After i is greater than 5, no case statements match, so the default statement is executed.
The "break" statement is not required. If the "break" is omitted, execution will proceed to the next "case." It is sometimes desirable to have multiple cases with no "break" statements in between. Take, for example, the following program:
public class JavaProgram { public static void main(String args[]) { int i; for (i = 0; i < 15; i++) { switch (i) { case 0: case 1: case 2: case 3: case 4: System.out.println("i is less than 5."); break; case 5: case 6: case 7: case 8: case 9: System.out.println("i is less than 10."); break; default: System.out.println("i is 10 or more."); } } } }
i is less than 5. i is less than 5. i is less than 5. i is less than 5. i is less than 5. i is less than 10. i is less than 10. i is less than 10. i is less than 10. i is less than 10. i is 10 or more. i is 10 or more. i is 10 or more. i is 10 or more. i is 10 or more.
The Java program iterates from 0 to 14, and a "switch" statement is used to evaluate the value of "i" during each iteration. The "switch" statement will execute different blocks of code and print different messages to the console depending on the value of "i."
Advantages of the "switch" statement in Java
- Readability: Using a "switch" statement instead of multiple "if-else" statements makes the code more readable and easier to understand.
- Faster Execution: Generally, the "switch" statement is faster than multiple "if-else" statements, especially when there are many conditions to evaluate.
- Code Simplifier: The "switch" statement can be used to simplify complex conditional statements and organize code.
Disadvantages of the "switch" statement in Java
- Limited Expression Evaluation: Because the "switch" statement only assesses the value of a single expression, complex conditions or expressions cannot be evaluated using it.
- Limited Case Expressions: Because the "switch" statement only supports a small number of case expressions, it can be challenging to manage large sets of conditions.
- No Partial Matches: Because the "switch" statement does not support partial matches, substring-containing strings or expressions cannot be matched using it.
- Limited Type Support: The "switch" statement can be less useful in some circumstances because it only supports a limited number of data types, including characters, integers, and enumerated types.
« Previous Tutorial Next Tutorial »