Learn how to use the switch statement to check if a character is a vowel.
To write a C/C++ program that takes a character as input and checks whether it is a vowel or consonant using the switch statement.
The switch statement is a multi-way decision-making statement in C/C++. It is used to execute one block of code from multiple alternatives. It is often more readable than multiple if-else if statements when comparing a single variable against many constant values.
In this practical, you will use the switch statement to check if a character is a vowel (A, E, I, O, U) or a consonant.
The vowels in English are:
if-else if statements.
Run the program:
Sample Input & Output:
| Syntax | Description |
|---|---|
switch(expression) |
Starts the switch statement |
case value: |
Checks if expression equals value |
break; |
Exits the switch block |
default: |
Executes if no case matches |
| Feature | if-else |
switch |
|---|---|---|
| Best for | Complex conditions | Single variable with many constant values |
| Readability | Less readable for many conditions | More readable for many conditions |
| Performance | Slower for many conditions | Faster (uses jump table) |
| Default case | else |
default |
| Example | if (ch == 'a') |
case 'a': |
switch statement checks if the character is a vowel.case statements are stacked together for each vowel.default case handles consonants and non-alphabetic characters.switch statement is cleaner and more readable for this type of check.
You have successfully written a C program that uses the switch statement to check whether a character is a vowel or consonant. You have learned how to use the switch statement for multi-way decision-making.
switch statement can only be used with integer or character expressions. The break statement is important to prevent falling through to the next case.
default case is optional but recommended to handle unexpected values.
ایک C/C++ پروگرام لکھنا جو صارف سے ایک حرف ان پٹ کے طور پر لے اور switch بیان کا استعمال کرتے ہوئے چیک کرے کہ آیا یہ حرفِ علت ہے یا حرفِ صحیح۔
switch بیان C/C++ میں ایک کثیر شاخی فیصلہ سازی کا بیان ہے۔ یہ متعدد اختیارات میں سے کوڈ کے ایک بلاک کو انجام دینے کے لیے استعمال ہوتا ہے۔ یہ اکثر ایک ہی متغیر کا متعدد مستقل اقدار سے موازنہ کرتے وقت متعدد if-else if بیانات سے زیادہ پڑھنے کے قابل ہوتا ہے۔
اس عملی میں، آپ switch بیان کا استعمال کرتے ہوئے چیک کریں گے کہ آیا کوئی حرف حرفِ علت (A, E, I, O, U) ہے یا حرفِ صحیح۔
انگریزی میں حروفِ علت یہ ہیں:
if-else if بیانات سے زیادہ صاف ہے۔
پروگرام چلائیں:
نمونہ ان پٹ اور آؤٹ پٹ:
| نحو | وضاحت |
|---|---|
switch(expression) |
switch بیان شروع کرتا ہے |
case value: |
چیک کرتا ہے کہ آیا expression value کے برابر ہے |
break; |
switch بلاک سے باہر نکلتا ہے |
default: |
اگر کوئی case میچ نہ ہو تو انجام دیتا ہے |
switch بیان چیک کرتا ہے کہ آیا حرف حرفِ علت ہے۔case بیانات ہر حرفِ علت کے لیے ایک ساتھ لگائے گئے ہیں۔default کیس حروفِ صحیح اور غیر حروفِ تہجی کو ہینڈل کرتا ہے۔switch بیان اس قسم کی جانچ کے لیے زیادہ صاف اور پڑھنے کے قابل ہے۔
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو switch بیان کا استعمال کرتے ہوئے چیک کرتا ہے کہ آیا کوئی حرف حرفِ علت ہے یا حرفِ صحیح۔ آپ نے کثیر شاخی فیصلہ سازی کے لیے switch بیان کا استعمال کرنا سیکھ لیا ہے۔
switch بیان صرف انٹیجر یا کریکٹر ایکسپریشن کے ساتھ استعمال کیا جا سکتا ہے۔ break بیان اگلے کیس میں جانے سے روکنے کے لیے ضروری ہے۔
default کیس اختیاری ہے لیکن غیر متوقع اقدار کو ہینڈل کرنے کے لیے تجویز کیا جاتا ہے۔