Infinite Loops in C++ – Types, Examples and Output C++ में अनंत लूप – प्रकार एवं उदाहरण
An infinite loop is a loop that continues executing indefinitely because its terminating condition never becomes false. It is also called an endless loop. Infinite loops can be created using for, while, and do-while statements. Infinite loops are sometimes intentional, such as in menu-driven programs, servers, and applications that continuously wait for user input. However, an unintended infinite loop may cause a program to run continuously without producing the expected result.
Infinite Loop ऐसा loop होता है जो लगातार execute होता रहता है क्योंकि इसकी termination condition कभी false नहीं होती। इसे Endless Loop भी कहा जाता है। C++ में for, while और do-while statements का उपयोग करके Infinite Loops बनाए जा सकते हैं। कुछ programs में Infinite Loop का उपयोग जानबूझकर किया जाता है, जैसे menu-driven programs, servers तथा ऐसे applications में जो लगातार user input की प्रतीक्षा करते हैं। लेकिन अनजाने में बनाया गया Infinite Loop प्रोग्राम को लगातार execute कर सकता है और अपेक्षित परिणाम नहीं मिल पाता।
1. Infinite for Loop
An infinite for loop is created when its condition is omitted or remains true forever. In C++, if the condition part is omitted, it is treated as true.
जब for loop की condition को हटा दिया जाता है या वह हमेशा true रहती है, तब Infinite for Loop बनता है। C++ में यदि condition वाले भाग को छोड़ दिया जाए, तो उसे true माना जाता है।
Syntax:
for (;;){statements;}
Example: C++ Program Using Infinite for Loop
#include <iostream>using namespace std;int main(){for (;;){cout << "Hello World" << endl;}return 0;}
Output:
Hello WorldHello WorldHello WorldHello World...
The loop continuously prints Hello World because no terminating condition is provided.
यह लूप लगातार Hello World print करता रहता है क्योंकि इसमें कोई terminating condition नहीं दी गई है।
2. Infinite while Loop
An infinite while loop is created when its condition always evaluates to true.
जब while loop की condition हमेशा true रहती है, तब Infinite while Loop बनता है।
Syntax:
while (true){statements;}
Example: C++ Program Using Infinite while Loop
#include <iostream>using namespace std;int main(){int i = 1;while (i <= 5){cout << "Welcome to C++" << endl;}return 0;}
Output:
Welcome to C++Welcome to C++Welcome to C++Welcome to C++...
In this program, the value of i is never updated. Therefore, the condition i <= 5 always remains true, and the loop continues indefinitely.
इस प्रोग्राम में i की value को update नहीं किया गया है। इसलिए i <= 5 condition हमेशा true रहती है और लूप लगातार execute होता रहता है।
3. Infinite do-while Loop
An infinite do-while loop is created when its condition always remains true.
जब do-while loop की condition हमेशा true रहती है, तब Infinite do-while Loop बनता है।
Syntax:
do{statements;}while (true);
Example: C++ Program Using Infinite do-while Loop
#include <iostream>using namespace std;int main(){int i = 1;do{cout << "Learning C++" << endl;}while (i <= 5);return 0;}
Output:
Learning C++Learning C++Learning C++Learning C++...
In this program, the value of i is never increased. Therefore, the condition i <= 5 always remains true, and the loop never terminates.
इस प्रोग्राम में i की value को बढ़ाया नहीं गया है। इसलिए i <= 5 condition हमेशा true रहती है और लूप कभी terminate नहीं होता।
4. How to Stop an Infinite Loop? (Infinite Loop को कैसे रोकें?)
An infinite loop can be stopped by using a break statement when a specific condition is satisfied. The program can also be terminated manually while it is running.
जब कोई specific condition पूरी हो जाती है, तब break statement का उपयोग करके Infinite Loop को रोका जा सकता है। प्रोग्राम के execution के दौरान उसे manually भी terminate किया जा सकता है।
Example: C++ Program to Stop a Loop Using break
#include <iostream>using namespace std;int main(){int i = 1;while (true){cout << i << " ";if (i == 5){break;}i++;}return 0;}
Output:
1 2 3 4 5Explanation (व्याख्या):
while(true)creates a loop whose condition is always true.The value of
iis printed during each iteration.When
ibecomes 5, thebreakstatement terminates the loop.Program control moves to the statement following the loop.
while(true)ऐसा loop बनाता है जिसकी condition हमेशा true रहती है।प्रत्येक iteration में
iकी value print होती है।जब
iकी value 5 हो जाती है, तबbreakstatement लूप को terminate कर देता है।इसके बाद प्रोग्राम का control लूप के बाद वाले statement पर चला जाता है।
5. Common Causes of Infinite Loops (Infinite Loop के सामान्य कारण)
- Missing Updation: The loop control variable is not updated.Updation का न होना: Loop control variable की value को update नहीं किया जाता।
- Incorrect Condition: The loop condition is written in such a way that it never becomes false.गलत Condition: लूप की condition इस प्रकार लिखी जाती है कि वह कभी false नहीं होती।
- Incorrect Logical Expression: A logical expression always evaluates to true because of an incorrect operator or condition.गलत Logical Expression: गलत operator या condition के कारण logical expression हमेशा true हो जाता है।
- Unintended Infinite Loop: A programmer intentionally creates a continuous loop but forgets to provide a proper termination mechanism.अनजाने में Infinite Loop: प्रोग्रामर लगातार चलने वाला loop बनाता है, लेकिन उसे terminate करने का उचित तरीका देना भूल जाता है।
Infinite loops are useful in programs that need continuous execution, such as servers, monitoring systems, and menu-driven applications. However, they should be designed carefully with a suitable termination mechanism to avoid unwanted execution.
Infinite Loops उन programs में उपयोगी होते हैं जिन्हें लगातार execute करने की आवश्यकता होती है, जैसे servers, monitoring systems और menu-driven applications। हालांकि, अनावश्यक execution से बचने के लिए इनमें उचित termination mechanism का होना आवश्यक है।
Comments
Post a Comment