एक program में control statement निम्नलिखित कारको के लिए प्रयोग किया जाता है-
- program को किसी स्थिति के अनुसार execute करने के लिए,
- program में किसी specific statement को छोड़कर या एक ही statement को कई बार execute करने के लिए ,
- किसी विशेष जैसे operation के तहत program को समाप्त करने के लिए, जैसे कई task perform करने के लिए हम C ++ में control statement का उपयोग करते हैं।
किसी भी Programming language में control statement का एक important role होता है। यह एक Basic concept है जिससे हम किसी task के लिए code/logics लिखते हैं।
Type of Control Statement in C++
C++ में control statement को तीन भागों में बांटा गया है।
- Conditional statement in C++
- Looping statement in C++
- Breaking statement in C++
Conditional statement in C++
selection statement में एक या एक से अधिक statement होते हैं जिन्हे parenthesis ब्रैकेट के अंदर declare किया जाता है और जिस ब्लॉक की expression value true होती है वह statement execute हो जाता है। इस प्रकार के statement को decision-making statement के नाम से भी जाना जाता है।
Type of conditional statement in C++
- if statement in C++
- if-else statement in C++
- switch statement in C++
Here is another one,
nested if-else
if statement in C++
इस statement में केवल एक single statement होती है जिसे body-of-if कहते हैं इसमें जैसे ही expression value true होती है body of if execute हो जाता है। अगर expression value false होती है तो execution if-statement से बहार program के अन्य statement में transfer हो जाता है (अगर program में कोई अन्य को statement है तो )
इसका syntax नीचे दिया गया है।
SYNTAX
if(expression/condition)
{
body of if;
}
एक if-statement का flow-diagram इस प्रकार का होगा।
Example of if statement in C++
नीचे दिए गए program में condition दी गई है कि अगर user input 5 से कम होता है तो ही body of if execute हो।
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x; // variable declaration
cout<<"Enter number: ";
cin>>x;
if(x<5) // expression
{
cout<<"Given input is smaller than 5: "<<x; // Body of if
}
getch();
}
OUTPUT
Given input is smaller than 5: 4
Explanation
- Program में user द्वारा दिया गया input variable x में store होता।
- और फिर if-statement में expression value true होती है जो कि true हो रही है इसलिए यहाँ पर body of if execute होता है।
if-else statement in C++
इसमें एक से अधिक स्टेटमेंट होते हैं। पहला body of if और दूसरा body of else कहलाता है। इसमें if-statement की तरह ही expression value true होने पर
body of if execute है लेकिन expression value false होने पर body-of-else execute होता है अर्थात यहाँ पर दोनों स्थितियों में execution होता है चाहे expression value true हो या false.
आप इसे नीचे दिए गए syntax से समझ सकते हैं।
SYNTAX:
if(expression/condition)
{
body of if;
}
else
{
body of else;
}
Flow-diagram of the if-statement,
Example of an if-else statement in C++
नीचे दिए गए program में condition दी गयी कि अगर user-input 5 से कम हो तो body of if execute होगा और अगर expression false होता है तो body of else execute हो और जैसे कि program का output देखकर पता चलता है कि Program में expression value true हो रही है इसलिए body of if execute होगा और body of else skip होगा।
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x; // variable declaration
cout<<"Enter number: ";
cin>>x;
if(x<5) // expression
{
cout<<"number smaller then 5"; // body of if
}
else {
cout<<"number greater then 5"; //body of else
}
getch();
}
OUTPUT
Enter number: 4
number smaller then 5
इसका execution आप नीचे Diagram में देख सकते हैं –
यहाँ एक अन्य उदाहरण दिया गया है –
creating a calculator using if-else in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2; // variable declaration
cout<<"Enter Two number: ";
cin>>num1>>num2;
char select;
cout<<"select operation(+,-,*,/): ";
cout<<select;
if(select=='+')
cout<<num1+num2;
else if(select=='-')
cout<<num1-num2;
else if(select=='*')
cout<<num1*num2;
else if(select=='/')
cout<<num1/num2;
getch();
}
OUTPUT
Enter two number: 4 8
select operation(+,-,*,/): 12
nested if-else statement in C++
यह कोई अन्य प्रकार का statement नहीं है। यह एक से अधिक if-else statement का समूह होता है। इसमें एक if-statement की body दूसरी if-else statement होती है। अर्थात body of if या body of else ही खुद एक if-else statement होती है।
इसे आप नीचे दिए गए syntax से समझ सकते हैं-
if(expression)
{
//outer if body
if(expression)
{
body of if; // inner if body
}
else
{
body of else // inner else body
}
} // close outer if body
else
{
body of else // outer else body
}
इसका flow diagram नीचे दिया गया है –
Explanation
nested if-statement में, पहला if-statement (outer if-statement) की body एक दूसरा if-statement होगा (जैसा कि ऊपर चित्र में दिखाया गया है)। जब outer if-statement में expression true होगा, तो execution inner if-statement में चला जाएगा और अगर यह गलत हो जाता है तब execution outer else body में जाएगा।
inner if-statement एक सामान्य if-statement की ही भांति होगा।
चलिए इसे एक अन्य उदाहरण से समझते हैं,
Example of a nested if-else statement in C++
इसे आप यहाँ नीचे दिए गए program से समझ सकते हैं।
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x;
cout<<"Enter number: ";
cin>>x;
if(x<5) // outer if statement
{
// outer if body
if(x<3) //inner if statement
{
cout<<"number greater than 3"; // inner if body
}else {
cout<<"number smaller than 3";//inner else body
}
}
else{
cout<<"outer else body execute"; // body of outer else
}
getch();
}
OUTPUT
Enter number: 2
number greater than 3
Enter number: 5
Outer else body execute
Related Exercise
- found out negative or positive number C++
- Find out in Given number Even or Odd In C++
- Find the largest number in any three given number in C++
<C++ type modifier in hindi
>switch statement in C++ in hindi