Use switch statement with a range of value in each case?

Program to use switch statement with a range of value in each case print the one statements at depend on multiple conditions.

  • Enter the use input from Scanner(System.in) object.
  • Here sc.nextInt() use to take only integer number.
  • In Switch case if we enter the number 4, then this number will be match with case 4 and print the statements.
  • Incase if number is not match to any switch case then it take default case and print the statements.
import java.util.Scanner;

public class SwitchRangeTest {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("enter the input number:-");
		int num = sc.nextInt();

		switch (num) {
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			System.out.println("Number range is 1 to 5");
			break;
		default:
			System.out.println("number is out of range");
		}
		sc.close();
	}
}

Output :-
enter the input number:-
2
Number range is 1 to 5