इस ट्यूटोरियल में, हम कुछ उदाहरणों की मदद से C ++ प्रोग्रामिंग में लूप करते समय और करते हैं … का उपयोग सीखेंगे।
कंप्यूटर प्रोग्रामिंग में, कोड के एक ब्लॉक को दोहराने के लिए लूप का उपयोग किया जाता है।
उदाहरण के लिए, मान लें कि हम 100 बार संदेश दिखाना चाहते हैं। फिर 100 बार प्रिंट स्टेटमेंट लिखने के बजाय, हम एक लूप का उपयोग कर सकते हैं।
यह सिर्फ एक सरल उदाहरण था; हम लूप्स का प्रभावी उपयोग करके अपने कार्यक्रमों में बहुत अधिक दक्षता और परिष्कार प्राप्त कर सकते हैं।
हैं 3 सी ++ में छोरों के प्रकार।
for
पाशwhile
पाशdo… while
पाश
पिछले ट्यूटोरियल में, हमने लूप के लिए C ++ के बारे में सीखा। यहां, हम while
और do… while
लूप के बारे में जानने जा रहे हैं ।
लूप करते समय सी ++
while
लूप का सिंटैक्स है:
while (condition) ( // body of the loop )
यहाँ,
- एक
while
लूप का मूल्यांकन करता हैcondition
- यदि
condition
मूल्यांकन करता हैtrue
, तोwhile
लूप के अंदर कोड निष्पादित होता है। condition
फिर से मूल्यांकन किया जाता है।- जब तक यह प्रक्रिया जारी रहती
condition
हैfalse
। - जब
condition
मूल्यांकन करता हैfalse
, तो लूप समाप्त हो जाता है।
के बारे में अधिक जानने के लिए conditions
, C ++ रिलेशनल और लॉजिकल ऑपरेटर्स पर जाएं।
लूप का फ्लोचार्ट
लूप करते समय C ++ का फ्लोचार्टउदाहरण 1: 1 से 5 तक संख्याएं प्रदर्शित करें
// C++ Program to print numbers from 1 to 5 #include using namespace std; int main() ( int i = 1; // while loop from 1 to 5 while (i <= 5) ( cout << i << " "; ++i; ) return 0; )
आउटपुट
1 2 3 4 5
यहां बताया गया है कि प्रोग्राम कैसे काम करता है।
Iteration | चर | i <= 5 | क्रिया |
---|---|---|---|
पहली बार | i = 1 | true | 1 छपा है और i बढ़ा है 2 । |
2 रा | i = 2 | true | 2 छपा है और i बढ़ा है 3 । |
३ | i = 3 | true | 3 मुद्रित किया जाता है और i इसे बढ़ाया जाता है4 |
४ था | i = 4 | true | 4 छपा है और i बढ़ा है 5 । |
5 वाँ | i = 5 | true | 5 छपा है और i बढ़ा है 6 । |
6 ठ | i = 6 | false | लूप को समाप्त कर दिया जाता है |
उदाहरण 2: केवल सकारात्मक संख्याओं का योग
// program to find the sum of positive numbers // if the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include using namespace std; int main() ( int number; int sum = 0; // take input from the user cout <> number; while (number>= 0) ( // add all positive numbers sum += number; // take input again if the number is positive cout <> number; ) // display the sum cout << "The sum is " << sum << endl; return 0; )
आउटपुट
एक नंबर दर्ज करें: 6 एक नंबर दर्ज करें: 12 एक नंबर दर्ज करें: 7 एक नंबर दर्ज करें: 0 एक नंबर दर्ज करें: -2 योग 25 है
इस कार्यक्रम में, उपयोगकर्ता को एक संख्या दर्ज करने के लिए कहा जाता है, जिसे चर संख्या में संग्रहीत किया जाता है।
संख्याओं के योग को संग्रहीत करने के लिए, हम एक चर राशि की घोषणा करते हैं और इसके मूल्य के लिए इसे आरंभ करते हैं 0
।
while
पाश जारी है जब तक उपयोगकर्ता एक ऋणात्मक संख्या में प्रवेश करती है। प्रत्येक पुनरावृत्ति के दौरान, उपयोगकर्ता द्वारा दर्ज की गई संख्या को योग चर में जोड़ा जाता है।
जब उपयोगकर्ता एक नकारात्मक संख्या में प्रवेश करता है, तो लूप समाप्त हो जाता है। अंत में, कुल योग प्रदर्शित होता है।
C ++ करते हैं … जबकि लूप
do… while
पाश का एक संस्करण है while
एक महत्वपूर्ण अंतर के साथ पाश: के शरीर do… while
पाश से पहले एक बार निष्पादित किया जाता है condition
की जाँच की है।
इसका सिंटैक्स है:
do ( // body of loop; ) while (condition);
यहाँ,
- लूप के शरीर को पहले निष्पादित किया जाता है। फिर
condition
मूल्यांकन किया जाता है। - यदि
condition
मूल्यांकन किया जाता हैtrue
, तोdo
कथन के अंदर लूप के शरीर को फिर से निष्पादित किया जाता है। condition
एक बार फिर से मूल्यांकन किया जाता है।- यदि
condition
मूल्यांकन किया जाता हैtrue
, तोdo
कथन के अंदर लूप के शरीर को फिर से निष्पादित किया जाता है। condition
मूल्यांकन करने तक यह प्रक्रिया जारी रहती हैfalse
। फिर लूप बंद हो जाता है।
लूप के फ्लोचार्ट… जबकि
लूप करते समय C ++ का फ्लोचार्ट…उदाहरण 3: 1 से 5 तक संख्या प्रदर्शित करें
// C++ Program to print numbers from 1 to 5 #include using namespace std; int main() ( int i = 1; // do… while loop from 1 to 5 do ( cout << i << " "; ++i; ) while (i <= 5); return 0; )
आउटपुट
1 2 3 4 5
यहां बताया गया है कि प्रोग्राम कैसे काम करता है।
Iteration | चर | i <= 5 | क्रिया |
---|---|---|---|
i = 1 | जाँच नहीं की गई है | 1 मुद्रित किया जाता है और i 2 तक बढ़ाया जाता है |
|
पहली बार | i = 2 | true | 2 छपा हुआ है और i 3 तक बढ़ा हुआ है |
2 रा | i = 3 | true | 3 छपा हुआ है और i 4 तक बढ़ा हुआ है |
३ | i = 4 | true | 4 मुद्रित किया जाता है और i 5 तक बढ़ाया जाता है |
४ था | i = 5 | true | 5 छपा है और i बढ़ाकर 6 किया गया है |
5 वाँ | i = 6 | false | लूप को समाप्त कर दिया जाता है |
उदाहरण 4: केवल सकारात्मक संख्याओं का योग
// program to find the sum of positive numbers // If the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include using namespace std; int main() ( int number = 0; int sum = 0; do ( sum += number; // take input from the user cout <> number; ) while (number>= 0); // display the sum cout << "The sum is " << sum << endl; return 0; )
आउटपुट 1
एक नंबर दर्ज करें: 6 एक नंबर दर्ज करें: 12 एक नंबर दर्ज करें: 7 एक नंबर दर्ज करें: 0 एक नंबर दर्ज करें: -2 योग 25 है
Here, the do… while
loop continues until the user enters a negative number. When the number is negative, the loop terminates; the negative number is not added to the sum
variable.
Output 2
Enter a number: -6 The sum is 0.
The body of the do… while
loop runs only once if the user enters a negative number.
Infinite while loop
If the condition
of a loop is always true
, the loop runs for infinite times (until the memory is full). For example,
// infinite while loop while(true) ( // body of the loop )
Here is an example of an infinite do… while
loop.
// infinite do… while loop int count = 1; do ( // body of loop ) while(count == 1);
In the above programs, the condition
is always true
. Hence, the loop body will run for infinite times.
for vs while loops
A for
loop is usually used when the number of iterations is known. For example,
// This loop is iterated 5 times for (int i = 1; i <=5; ++i) ( // body of the loop )
Here, we know that the for-loop will be executed 5 times.
हालाँकि, while
और do… while
लूप आमतौर पर उपयोग किए जाते हैं जब पुनरावृत्तियों की संख्या अज्ञात होती है। उदाहरण के लिए,
while (condition) ( // body of the loop )
अधिक जानने के लिए इन उदाहरणों को देखें:
- सी ++ प्रोग्राम फाइबोनैचि श्रृंखला प्रदर्शित करने के लिए
- C ++ प्रोग्राम GCD खोजने के लिए
- सी ++ प्रोग्राम एलसीएम खोजने के लिए