हिंदी माध्यम नोट्स
C++ : Nested if और switch Statement with program example explanation in hindi , what is Nested if and switch Statement
जब दो या दो से अधिक condition को check किया जाता है | तब if statement के nesting को use किया जाता है | if statement के nesting को निन्म syntax को use किया जाता है |
if (condition 1)
{
body 1 ;
}
elseif(condition 2 )
{
body 2 ;
}
elseif(condition 3 )
{
body 3 ;
}
……………….
……………….
……………….
elseif(condition n )
{
body n;
}
else{
body n;
}
यहा पर
condition 1 ; ये condition 1 को check किया जाता है |
condition 2; ये condition 2 को check किया जाता है |
condition 3 ; ये condition 3 को check किया जाता है |
condition 4 ; ये condition 4 को check किया जाता है |
…………………………………….
…………………………………….
condition n ; ये condition n को check किया जाता है |
जब condition 1 true होगी तब body 1 को execute किया जाता है | अन्यथा condition 2 को check किया जाता है |
जब condition 2 true होगी तब body 2 को execute किया जाता है | अन्यथा condition 3 को check किया जाता है |
जब condition 3 true होगी तब body 3 को execute किया जाता है | अन्यथा condition 4 को check किया जाता है |
ये loop तब तक चलता है | जब तक condition n को check किया जाता है |
जब condition true होगी तब body n को execute किया जाता है | अन्यथा body n execute होता है |
इसका उदाहरन होता है :-
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int a,b,c;
cout<<“Enter Value 1 ” :
cin>>a;
cout<<“Enter Value 2 ” :
cin>>b;
cout<<“Enter Value 3 ” :
cin>>c;
if(a>b)
{
if(a>c)
cout<<“Maximum Value :”<<a;
else
cout<<“Maximum Value :”<<c;
}
elseif(c>b)
{
cout<<Maximum Value :”<<c;
}
else
{
cout<<Maximum Value :”<<b;
}
getch();
}
इस उदाहरन मे , तीन values मे maximum value को find करते है | इसमें
सबसे पहले a>c की condition को check किया जाता है | और अगर condition true होती है तब a>c की condition की check की जाती है | अगर condition true होती है तब maximum value : a display होती है | और maximum value : c display होती है |
अन्यथा c>b की condition check की जाती है | अगर condition true होती है तब Maximum Value :c print होगा | अन्यथा Maximum Value :b display होगा |
Switch statement
किसी प्रोग्राम मे multple condition को check करने के लिए nested if statement के अलावा switch statement को use किया जा सकता है | switch statement का syntax होता है :-
switch ( case_lable )
{
case case_lable 1 : body 1 ;
break ;
case case_lable 2 : body 2 ;
break ;
case case_lable 3 : body 3 ;
break ;
case default : body ;
break ;
}
switch statement निन्म keyword होते है :
case lable : ये case lable है जो की condition के true होने पर body को पेर्व्फोर्म किया जाता है |
case_lable 1 : ये condition की value होती है जो की body 1 को perform करता है |
case_lable 2 : ये condition की value होती है जो की body 2 को perform करता है |
case_lable 3 : ये condition की value होती है जो की body 3 को perform करता है |
switch statement मे default case को जरुर लिखना चाहिए |अगर condition मे मे से कोई भी value प्राप्त होती है तब default case मे लिखा body perform होता है |
यहा case _lable किसी excpression का आउटपुट हो सकता है | या condition की value भी हो सकती है |
उदाहरन के लिए :
int a<b ; इस statement से दो case हो सकते है ‘1’ या ‘0’ होगी |
और
a+b ; यह पर variable a और b की addition से आउटपुट मिलता है | इस आउटपुट को switch statement मे case lable को बनाया जाता है |
इसका उदाहरन होता है :
इसका उदाहरन होता है :-
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char choice ;
int a,b,c ;
cout<<“Enter ‘a’ for addition “;
cout<<“Enter ‘s’ for subtraction”;
cout<<“Enter ‘m’ for multiplication “;
cout<<“Enter ‘d’ for divide “;
cout<<“Enter Choice : “;
cin>>choice ;
cout<<“Enter Input 1 : “;
cin>>a;
cout<<“Enter Input 2 : “;
cin>>b;
switch(choice)
{
case ‘a’ :
c=a+b;
break;
case ‘s’ :
c=a-b;
break;
case ‘m’ :
c=a*b;
break;
case ‘d’ :
c=a/b;
break;
defalut :
break;
}
cout<<“Output : “<<c;
getch();
}
getch();
}
इस उदाहरन मे यूजर द्वारा choice को input किया जाता है |अगर यूजर द्वारा input किया गया input निन्म होता है तब
‘a’ : variable ‘a’ और ‘b’ मे addition perform होता है |
‘s’ : variable ‘a’ और ‘b’ मे subtraction perform होता है |
‘d’ : variable ‘a’ और ‘b’ मे divind perform होता है |
‘m’ : variable ‘a’ और ‘b’ मे multiplication perform होता है |
इदका आउटपुट होगा :-
Enter ‘a’ for addition
Enter ‘s’ for subtraction
Enter ‘m’ for multiplication
Enter ‘d’ for divide
Enter Choice : a
Enter Input 1 : 12
Enter Input 2 : 23
Output : 35
इस article मे nested if statement और switch statement को discuss किया है | अब अगले article मे switch statement पर based कुछ और उदाहरनो को discuss करेगे |
Recent Posts
सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है
सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…
मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the
marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…
राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi
sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…
गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi
gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…
Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन
वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…
polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten
get all types and chapters polity notes pdf in hindi for upsc , SSC ,…