In this example we will check the character is a vowel or consonant
#include <iostream>
using namespace std;
int main(){
char ch;
int lowerCase,upperCase;
cout<<"Enter character"<<endl;
cin>>ch;
// evaluates to 1 if variable c is a lowercase vowel
lowerCase = (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'o' || ch == 'u');
// evaluates to 1 if variable c is a UpperCase vowel
upperCase = (ch == 'A' || ch == 'I' || ch == 'E' || ch == 'O' || ch == 'U');
if(lowerCase || upperCase)
cout<<ch<< " is Vowel"<<endl;
else
cout<<ch<<" is Consonant"<<endl;
return 0;
}
User Input: a
The code checks:
lowerCase = (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'o' || ch == 'u')
→ This becomes true becausech == 'a'
.upperCase = (ch == 'A' || ch == 'I' || ch == 'E' || ch == 'O' || ch == 'U')
→ This becomes false becausech == 'A'
is not true.
Since one of the conditions is true (lowerCase
is true), the program prints:
Output:
a is Vowel