Selection Statements: if and else
if keyword का उपयोग किसी statement या block को execute करने के लिए किया जाता है, यदि, और केवल तभी, जब कोई condition, true होती है। इसका Syntax है:
if (condition) statement:
यहाँ, condition वह expression है जिसका evaluation किया जा रहा है। यदि यह condition सत्य है, तो condition execute किया जाता है। यदि यह false है, तो condition execute नहीं किया जाता है (इसे ignore कर दिया जाता है), और Program पूरे selection statement के ठीक बाद Continue रहता है।
Example के लिए, following code fragment message को print करता है (x 100 है), केवल तभी x variable में stored value वास्तव में 100 है।
if (x == 100) cout << "x is 100";
यदि x exactly 100 नहीं है, तो इस condition पर ध्यान नहीं दिया जाता है, और कुछ भी print नहीं होता है।
यदि आप condition पूरी होने पर execution के लिए एक से अधिक condition को include करना चाहते हैं, तो इन conditions को braces ({}) में enclosed किया जाएगा, जिससे एक block बन जाएगा।
if (x == 100)
{
cout << "x is ";
cout << x;
}
As usual, code में indentation और line break का कोई प्रभाव नहीं पड़ता है, इसलिए above codeको निम्नलिखित तरीके से भी लिख सकते है।
|
if के साथ Selection Statement यह भी निर्दिष्ट कर सकता है कि Alternative Statement प्रस्तुत करने के लिए else keyword का उपयोग करते है, जब Condition Complete नहीं होती है।
if (condition) Statement1 else Statement2
Example :
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
यह x is 100 print करता है, अगर वास्तव में x की value 100 है, लेकिन अगर ऐसा नहीं है, तो यह x is not 100 प्रिंट करता है
Iteration Statements (Loops) :
Loops एक statement को एक certain number तक repeat हैं, या जब कोई condition complete होती है। They are introduced by the keywords while
, do
, and for
.
1. while loop :
while loop, statement को तब तक repeat करता है जब तक Given expression true है। यदि, किसी condition के execution के बाद, expression, true नहीं है, तो loop समाप्त हो जाता है, और loop के ठीक बाद program continue रहता है। उदाहरण के लिए-
// custom countdown using while
#include <iostream>
using namespace std;
int main ()
{
int n = 5;
while (n>0) {
cout << n << ", ";
--n;
}
cout << "Thanks to visit Diploma warriors!\n";
}
Previous program की whole process को निम्नलिखित script के अनुसार व्याख्या (interpreted) किया जा सकता है :
- n को एक value assign किया गया है
- जबकि स्थिति की जाँच की जाती है (n> 0)। इस point पर दो possibilities हैं:
- Condition, true है: कथन execute किया गया है (step 3 तक)
- Condition, false है: ignore the condition और उसके बाद continue रखें (step 5 तक)
- Execute statement:
- cout << n << “,”;
- –n;
- (prints the value of
n
and decreasesn
by 1) - End of block। and automatically step 2 पर return होता है । block के ठीक बाद program continue रखें:
- print
Thanks to visit Diploma warriors
! और Program end करें।
2. do-while loop:
यह while loop की तरह behave करता है, सिवाय इसके कि condition का evaluation पहले के बजाय statement के execution के बाद किया जाता है, कम से कम एक statement के execution की guarantee देता है, भले ही condition कभी fulfilled न हो। उदाहरण के लिए, निम्न example user द्वारा introduced किए गए किसी भी text को प्रतिध्वनित(echoes) करता है जब तक कि user goodbye enter नहीं करता-
// echo machine
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str; do {
cout << "Enter text: ";
getline (cin,str);
cout << "You entered: " << str << '\n';
} while (str != "goodbye");
}
while को कम से कम एक बार निष्पादित करने की आवश्यकता होती है, तो do-while loop को आमतौर पर पसंद किया जाता है, जैसे कि जब loop के अंत तक check की गई condition को loop के भीतर ही declare किया जाता है।
पिछले Examples में, block के भीतर user input वह है जो loop end होने पर निर्धारित करेगा। और इस प्रकार, भले ही user goodbye enter करके loop को जल्द से जल्द समाप्त करना चाहता हो, loop में block को input के लिए संकेत देने के लिए कम से कम एक बार execute करने की आवश्यकता होती है, और स्थिति वास्तव में इसके बाद ही निर्धारित की जा सकती है execute किया जाता है।
3. for loop :
for loop को मल्टीप्ल types रिपीट करने के लिए design किया गया है।
Syntax:
for (initialization; condition; increase) statement;
while loop तरह, यह loop statement को repeat करता है जबकि condition true है। लेकिन, इसके अलावा, loop के लिए specific location प्रदान करता है जिसमें एक initialization और एक increment अभिव्यक्ति होती है, जिसे loop के पहली बार start होने से पहले और प्रत्येक iteration के बाद क्रमशः execute किया जाता है। इसलिए, condition के रूप में counter variable का उपयोग करना विशेष रूप से उपयोगी है।
for ( n=0, i=100 ; n!=i ; ++n, --i )
{
// whatever here...
This loop will execute 50 times if neither n or i are modified within the loop:
n 0 के मान से start होता है, और i = 100 के साथ, condition है n!=i (अर्थात, वह n, i के equal नहीं है)। क्योंकि n प्रत्येक iteration पर एक से increase हो जाता है, और i एक से decrease हो जाता है, loop की स्थिति 50 वें iteration के बाद false हो जाएगी, जब n और i दोनों 50 के equal होंगे।
Jump Statement :
jump statement, specific locations पर jump करके program के flow को बदलने की अनुमति देता है।
The break statement :
Break एक loop छोड़ देता है, भले ही इसके अंत की condition पूरी न हुई हो। इसका उपयोग infinite loop को end करने के लिए किया जा सकता है, या इसे अपने natural end से पहले end करने के लिए forced किया जा सकता है। उदाहरण के लिए, आइए reverse counting को उसके natural end से पहले रोक दें:
// break loop example
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n--)
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
The continue statement:
Continue statement, Program को rest of loop को current iteration में छोड़ने का कारण बनता है, जैसे कि कथन ब्लॉक का अंत पहुंच गया था, जिससे यह निम्नलिखित पुनरावृत्ति की शुरुआत में कूद गया। उदाहरण के लिए, हमारी उलटी गिनती में नंबर 5 को छोड़ दें: