Check Vowel Using Switch in Java
beginner· Control Flow · Switch Statement
Problem
Stacking every vowel — both cases — under one switch block lets a single assignment cover all ten matching characters at once.
Given a single character, determine whether it is a vowel or a consonant.
Input
ch = 'o'
Output
'o' is a vowel
Java Program
Java
public class VowelCheckSwitch {
public static void main(String[] args) {
char ch = 'o';
boolean isVowel;
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u': // lowercase vowels
case 'A': case 'E': case 'I': case 'O': case 'U': // uppercase vowels
isVowel = true;
break;
default:
isVowel = false;
}
System.out.println("'" + ch + "' is a " + (isVowel ? "vowel" : "consonant"));
}
}Output
'o' is a vowel
Core Logic
Stacking all ten vowel characters — both cases — under a single case block, with every non-vowel falling through to default, decides vowel or consonant in one switch.
How It Works
- 1
case 'a': case 'e': ... case 'U':stacks all five vowels in both lowercase and uppercase under one block. - 2Any character matching one of those ten labels falls into the shared block and sets
isVowel = true. - 3The
defaultcase catches everything else and setsisVowel = false.
For
ch = 'o', it matches one of the stacked vowel labels, so isVowel becomes true.💡
Key Point: Stacking ten case labels with no code between them is what lets a single isVowel = true; cover every vowel — the labels share the block precisely because they share the same outcome.
Key Concepts
switch statementstacked case labelsdefault case