Menu
Topics Index
...
`

Print Alphabets Classification - Java Example Program


Print Alphabets Classification
class AlphabetsClassification
{
    public static void main(String arg[])
    {
        char alphabet = 'A';
        
        switch(alphabet)
        {
            case 'A':
                System.out.println("The alphabet '" + alphabet + "' is in first four letters");
                // LINE A
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                System.out.println("The alphabet '" + alphabet + "' is an vowel");
                break;
            case 'B':
            case 'C':
            case 'D':
                System.out.println("The alphabet '" + alphabet + "' is in first four letters");
            default:
                System.out.println("The alphabet '" + alphabet + "' is a consonant");
        }    
    }
}
OUTPUT

The alphabet 'A' is in first four letters
The alphabet 'A' is an vowel

DESCRIPTION

This program classifies a given alphabet and prints whether it is in the first four letters and whether it is a vowel or a consonant. Here we can observe that we are not using the break statement for every case statement. When there is no break, the execution continues until it encounters a break or if it reaches the end of the switch statement. That is the reason why when the alphabet is 'A', it prints both The alphabet 'A' is in first four letters and The alphabet 'A' is an vowel. At LINE A, it simply falls through to the next case.

THINGS TO TRY
  • Change the value of variable alphabet to various values like 'B', 'E', 'K' etc., and observe the output.

© meritcampus 2019

All Rights Reserved.

Open In App