switch statement, if-else
स्टेटमेंट की तरह ही है, लेकिन if-else में जहाँ line of code बढ़ जाता है जिसे program थोड़ा जटिल बन जाता है।
C ++ में इस समस्या को हल करने के लिए, हम Program में switch-statement को उपयोग में लाते हैं। switch-statement में execution step by step होता है और हर statementbreak
(default statement को छोड़कर) से terminate होता है , जिस statement में expression value true होती है, वह statement execute हो जाता है।
इसमें एक statement , default statement होता है जो तब execute होता है जब सभी statements की expression value false होती है।
SYNTEX:
switch (input)
{
case <1-exp>: first case body; break;
case <2-exp>: second case body; break;
case <3-exp>: third case body; break;
default: body of default;
};
flow-diagram of a switch-statement,
यहाँ पर switch – statement का flow diagram दिया गया है –
इसका program नीचे दिया गया है –
switch statement Example in in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int input;
cout<<"Enter a number: ";
cin>>input;
switch(input)
{
case 1: cout<<"1st statement"; break;
case 2: cout<<"2nd statement";break;
case 3: cout<<"3rd statement";break;
default: cout<<"Try again..";
}
getch();
}
OUTPUT
Enter a number: 2
2nd statement
चलिए इसे अन्य उदाहरण के साथ समझते हैं –
Example of switch statement with char data-type
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char input;
cout<<"Enter a character: ";
cin>>input;
switch(input) {
case 'a': cout<<"1st statement executed"; break;
case 'b': cout<<"2nd statement executed"; break;
case 'c': cout<<"3rd statement executed"; break;
default: cout<<"Try again..";
}
getch();
}
OUTPUT
Enter a character: b
2nd statement executed
switch – statement में, एक expression value int
, char
या special character हो सकते हैं।
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char input;
cout<<"Enter any value: ";
cin>>input;
switch(input)
{
case 1: cout<<"1st statement"; break;
case 'a': cout<<"entered value is a";break;
case 2: cout<<"entered value is 2";break;
case '/': cout<<" your entered / ";break;
case 'A': cout<<"entered value is A";break;
default: cout<<"Try again..";
}
getch();
}
OUTOUT
Enter any value: A
entered value is A
Explanation
जैसा कि पहले ही बताया गया है कि C++ एक sensitive language है जिसका अर्थ है कि इसमें “a” और “A ” दोनों अलग अलग input होंगे।
इसलिए input value same होने पर भी output डिफरेंट होगा (case sensitive के कारण ). परन्तु अगर हम चाहते है “a” और “A” का output एक जैसा हो तो,
switch(char data-type)
{
case 'a':
case 'A': cout<<"statement executed.."; break;
........
}
creating a calculator using switch statement in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2;
char input;
cout<<"Enter any two number: ";
cin>>num1>>num2;
cout<<"Select operation(+ , - , * , /): ";
cin>>input;
switch(input)
{
case '+': cout<<num1+num2; break;
case '-': cout<<num1-num2; break;
case '*': cout<<num1*num2; break;
case '/': cout<<num1/num2; break;
default: cout<<"Try again..";
}
getch();
}
OUTPUT
Enter any two number: 2 6
Select operation(+ , - , * , /): +
8
switch statement can not be of a float data type.
without break keyword switch statement in C++
यदि break keyword का उपयोग switch statement में नहीं जाए तो,
switch(input)
{
case 1: cout<<"1st statement";
case 2: cout<<"2nd statement";
case 3: cout<<"3rd statement";
default: cout<<"Try again..";
}
अब यदि user number 1 enter करता है, तो OUTPUT निम्नानुसार होगा,
1st statement
2nd statement
3rd statement
मतलब सभी statements को एक-एक करके execute होंगे लेकिन अगर user number 3 enter करता है तो OUTPUT अलग होगा तो केवल 3rd statement ही execute
होगा-
3rd statement
यदि case -3 के बाद और case -statements होते , तो वे सभी एक एक करके execute हो जाते (चाहे कंडीशन true हो या false ) ऐसा इसलिए क्योंकि यहाँ पर break keyword का प्रयोग नहीं किया गया।
creating a menu using switch statement in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int choice;
int num1,num2,result;
cout<<"1.Sum \n";
cout<<"2.Subtraction\n";
cout<<"3.Multiply\n";
cout<<"\nEnter Your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter Two Number: ";
cin>>num1>>num2;
result = num1+num2;
cout<<"\nTotal: "<<result;
break;
case 2:
cout<<"\nEnter Two Number: ";
cin>>num1>>num2;
result = num1-num2;
cout<<"\nSubstraction: "<<result;
break;
case 3:
cout<<"\nEnter Two Number: ";
cin>>num1>>num2;
result = num1*num2;
cout<<"\nMultiply: "<<result;
break;
default:
cout<<"\ninvalid choice....";
}
getch();
}
OUTPUT
1.Sum of two Number
2.Subtraction of two Number
3.Multiply of two Number
Enter Your Choice:1
Enter Two Number:4 5
Total: 9
एक अच्छे programming practice में, एक जैसे code को बार-बार लिखना अच्छी आदत नहीं होती।
जैसा कि हम उपरोक्त program में देख सकते हैं, एक ही statement को हर case-statement में बार-बार दोहराया गया है,
cout<<"\nEnter Two Number: ";
cin>>num1>>num2;
जिसे कि अच्छा programming practice नहीं माना जाएगा।
यह program, function द्वारा और बेहतर तरीके से बनाया गया है। जिसे आप यहाँ देख सकते हैं switch with function in C++
आपने देखा है, switch से संबंधित program एक menu की तरह व्यवहार कर रहा है इसलिए, C++ में , switch-statement का उपयोग program में menu रिलेटेड program बनाने के लिए किया जाता है।