🔹 Introduction
In Java, decision-making statements help control the flow of a program based on conditions. One such statement is the switch case, which is commonly used when multiple fixed values need to be checked.
In this blog, we will see how to write a Java program to check whether a given character is a vowel or a consonant using the switch statement.
🔹 Problem Statement
Write a Java program that:
• Stores a character
• Checks whether the character is a vowel
• Prints “Vowel” if the character is a vowel
• Prints “Consonant” otherwise
🔹 Java Program: Vowel or Consonant Using Switch Case
public class VowelOrConsonant {
public static void main(String[] args) {
char ch='u';
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("Vowel");
break;
default:
System.out.println("Consonant");
}
}
}
🔹 Sample Output:
Vowel
🔹 Explanation
• A character variable ch is initialized with the value 'u'
• The switch statement compares ch with vowel characters
• Multiple case labels are used to handle all vowels
• If a match is found, “Vowel” is printed
• If no case matches, the default block executes and prints “Consonant”
• The break statement stops further execution after a match
🔹 Why Use Switch Case Here?
• Makes the code clean and readable
• Avoids multiple if-else conditions
• Suitable when checking a variable against fixed values
Top comments (0)