Group Switch Statement in Java


The switch statement is Java's multi-way branch statement. It is used to take the place of long if-else chains, and make them more readable. However, unlike if statements, one may not use inequalities; each value must be concretely defined. The break statement is optional. If you omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them. For example, consider the following program:

Syntax:
    switch(expression)
    {
    case 1 :
    case 2 :
    case 3 :
    case 4 :
         // code inside the combined case
         break;
     case 5 :
    case 6 :
         // code inside the combined case value
         break;
     .
     .
     default :
         // code inside the default case .
    }

Source Code

import java.util.Scanner;
 
//Group Switch
public class group_switch {
    public static void main(String args[]) {
       char c;
        System.out.println("Enter The Character : ");
        Scanner in =new Scanner(System.in);
        c=in.next().charAt(0);
 
        switch (c)
        {
            case 'a':
            case 'e':
            case 'i':
            case '0':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                System.out.println(c + " is a Vowels");
                break;
            default:
                System.out.println(c + " is Consonant");
                break;
        }
 
    }
}
 

Output

Enter The Character :
u
u is a Vowels
To download raw file Click Here

Basic Programs