How to use a switch statement with ranges in Java?

Jan 05, 2026

Leave a message

Using a switch statement with ranges in Java can significantly enhance the efficiency and readability of your code, especially when dealing with a large number of conditional checks. As a switch supplier, I've seen firsthand how proper implementation of such programming techniques can streamline operations in various industries. In this blog, I'll guide you through the process of using a switch statement with ranges in Java, and also touch on how our high - quality switches can complement your programming projects.

Understanding the Basics of Switch Statements in Java

Before diving into switch statements with ranges, let's quickly recap the traditional switch statement in Java. A switch statement is a multi - way branch statement that provides an efficient way to dispatch execution to different parts of your code based on the value of an expression.

int num = 2;
switch (num) {
    case 1:
        System.out.println("The number is 1");
        break;
    case 2:
        System.out.println("The number is 2");
        break;
    default:
        System.out.println("The number is neither 1 nor 2");
}

In the above example, the switch statement checks the value of the variable num. If num is 1, it executes the code block under case 1. If num is 2, it executes the code block under case 2. The default block is executed when none of the specified cases match.

The Limitation of Traditional Switch Statements

The traditional switch statement in Java can only handle discrete values. It cannot directly handle ranges of values. For example, if you want to check if a number is in the range of 1 - 10, 11 - 20, etc., the traditional switch statement won't work out - of - the - box.

Using Switch Statements with Ranges in Java

To use a switch statement with ranges in Java, we can leverage the fact that we can map a range of values to a single representative value. Here's an example:

int score = 85;
int range;
if (score >= 0 && score < 60) {
    range = 1;
} else if (score >= 60 && score < 80) {
    range = 2;
} else if (score >= 80 && score <= 100) {
    range = 3;
}

switch (range) {
    case 1:
        System.out.println("Your score is in the 0 - 59 range.");
        break;
    case 2:
        System.out.println("Your score is in the 60 - 79 range.");
        break;
    case 3:
        System.out.println("Your score is in the 80 - 100 range.");
        break;
    default:
        System.out.println("Invalid score.");
}

In this example, we first determine which range the score variable falls into and assign a representative integer (range) to it. Then, we use a traditional switch statement on this representative integer to execute the appropriate code block.

Advanced Approaches

Another approach is to use Java 14's enhanced switch expressions. The enhanced switch expressions allow for more concise and powerful syntax.

int score = 90;
String result = switch ((score >= 0 && score < 60)? 1 : (score >= 60 && score < 80)? 2 : (score >= 80 && score <= 100)? 3 : 0) {
    case 1 -> "Your score is in the 0 - 59 range.";
    case 2 -> "Your score is in the 60 - 79 range.";
    case 3 -> "Your score is in the 80 - 100 range.";
    default -> "Invalid score.";
};
System.out.println(result);

This code uses a ternary operator to map the score to a representative value and then uses an enhanced switch expression to get the appropriate result string.

Complementing Your Programming with Our Switches

As a switch supplier, we offer a wide range of high - quality switches that can be used in various industrial and programming - related applications. For example, our 514120 Actuator is a reliable component that can be integrated into systems where precise control is required. It can be used in conjunction with software programs that use switch statements to control different states of a system.

Our 6GK5124 - 0BA00 - 2AB2 Electrical Switch is designed for electrical systems and can be used in projects where electrical circuits need to be controlled based on certain conditions, just like how a switch statement in Java controls the flow of code based on conditions.

The 6GK5116 - 0BA00 - 2AB2 Ethernet Switch XB116 is ideal for network - related projects. In a network management software, you can use switch statements to handle different network states, and our Ethernet switch can be the physical device that implements those states in the network.

Conclusion

Using a switch statement with ranges in Java can make your code more organized and easier to maintain. By mapping ranges to representative values, you can effectively use the power of switch statements in scenarios where ranges need to be checked.

514120 Actuator6GK5116-0BA00-2AB2 Ethernet Switch XB116

If you're interested in our high - quality switches for your programming or industrial projects, we invite you to reach out for procurement and further discussions. Our team of experts is ready to assist you in finding the right switch solutions for your specific needs.

References

  • Oracle Java Documentation
  • Effective Java by Joshua Bloch

Send Inquiry