while Loop in C++ – Syntax, Working and Examples with Output | C++ में while लूप – सिंटैक्स एवं उदाहरण
A while loop is an entry-controlled loop in C++. It repeatedly executes a block of statements as long as the given condition is true. Unlike a for loop, a while loop contains only the condition in its header. Initialization is generally performed before the loop, and updation is written inside the loop body.
C++ में while loop एक Entry-Controlled Loop है। यह दिए गए statements को तब तक बार-बार execute करता है, जब तक दी गई condition सत्य होती है। for loop के विपरीत, while loop के header में केवल condition लिखी जाती है। Initialization सामान्यतः लूप से पहले तथा Updation लूप के अंदर किया जाता है।
A while loop is especially useful when the number of iterations is not known in advance and depends on a condition.
while loop का उपयोग विशेष रूप से तब किया जाता है, जब प्रोग्रामर को पहले से यह पता न हो कि लूप कितनी बार execute होगा और इसकी repetitions किसी condition पर निर्भर करती हों।
Syntax of while Loop (व्हाइल लूप का सिंटैक्स)
initialization;while (condition){statements;updation;}
Parts of while loop (व्हाइल लूप के भाग):
- Initialization: It initializes the loop control variable before the loop starts.Initialization: इसमें लूप शुरू होने से पहले loop control variable को प्रारंभिक value दी जाती है।
- Condition: It is checked before every iteration. If the condition is true, the loop body executes; otherwise, the loop terminates.Condition: इसे प्रत्येक iteration से पहले check किया जाता है। Condition true होने पर लूप execute होता है, अन्यथा लूप समाप्त हो जाता है।
- Updation: It changes the value of the loop control variable inside the loop body.Updation: इसमें लूप के अंदर loop control variable की value को बदला जाता है।
Working of while Loop (व्हाइल लूप की कार्यप्रणाली)
The execution of a while loop follows these steps:
while loop का execution निम्नलिखित चरणों में होता है:
Initialization is performed before the loop.
The condition is checked.
If the condition is true, the loop body is executed.
The updation statement is executed.
The condition is checked again. This process continues until the condition becomes false.
सबसे पहले लूप के बाहर Initialization किया जाता है।
इसके बाद Condition check होती है।
यदि Condition true है, तो लूप के statements execute होते हैं।
इसके बाद Updation statement execute होता है।
Condition दोबारा check की जाती है। यह प्रक्रिया Condition false होने तक चलती रहती है।
Example 1: C++ Program to Print the Table of a Number Using while Loop
इस उदाहरण में while loop का उपयोग करके किसी संख्या का पहाड़ा 1 से 10 तक print किया गया है।
#include <iostream>using namespace std;int main(){int i, n;cout << "Enter a Number: ";cin >> n;cout << "Table of " << n << endl;i = 1;while (i <= 10){cout << n << " x " << i << " = " << n * i << endl;i++;}return 0;}
Output:
Enter a Number: 5Table of 55 x 1 = 55 x 2 = 105 x 3 = 155 x 4 = 205 x 5 = 255 x 6 = 305 x 7 = 355 x 8 = 405 x 9 = 455 x 10 = 50
Explanation (व्याख्या):
i = 1initializes the loop control variable.i <= 10checks the loop condition.i++increases the value ofiby 1 after each iteration.The loop terminates when
ibecomes 11.i = 1लूप control variable को initialize करता है।i <= 10लूप की condition check करता है।i++प्रत्येक iteration के बादiकी value को 1 बढ़ाता है।जब
iकी value 11 हो जाती है, तब लूप समाप्त हो जाता है।
Example 2: C++ Program for Counting from 1 to 100 Using while Loop
इस उदाहरण में while loop की सहायता से 1 से 100 तक की counting print की गई है। setw() manipulator का उपयोग numbers को व्यवस्थित रूप से प्रदर्शित करने के लिए किया गया है।
#include <iostream>#include <iomanip>using namespace std;int main(){int i = 1;cout << "Counting from 1 to 100" << endl;while (i <= 100){cout << setw(3) << i << " ";if (i % 10 == 0){cout << endl;}i++;}return 0;}
Output:
Counting from 1 to 1001 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 7071 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 9091 92 93 94 95 96 97 98 99 100
Explanation (व्याख्या):
int i = 1initializes the counting variable.i <= 100allows the loop to execute whileiis less than or equal to 100.setw(3)sets the minimum field width to 3 characters for the next output value.i % 10 == 0checks whether the current number is divisible by 10. If true, the next number is printed on a new line.i++increments the counting variable.int i = 1counting variable को initialize करता है।i <= 100condition के true रहने तक लूप execute होता है।setw(3)अगले output value के लिए minimum field width 3 characters निर्धारित करता है।i % 10 == 0check करता है कि वर्तमान संख्या 10 से divisible है या नहीं। यदि condition true होती है, तो अगली संख्या नई line में print होती है।i++counting variable की value को 1 बढ़ाता है।
Difference Between while and for Loop (while और for लूप में अंतर)
| Basis | while Loop | for Loop |
|---|---|---|
| Structure | Contains the condition in its header. | Contains initialization, condition and updation in its header. |
| Initialization | Generally written before the loop. | Generally written in the loop header. |
| Updation | Generally written inside the loop body. | Generally written in the loop header. |
| Use | Useful when the number of iterations is not known in advance. | Useful when the number of iterations is known in advance. |
आधार | while Loop | for Loop |
|---|---|---|
| संरचना | Header में केवल condition होती है। | Header में initialization, condition और updation होते हैं। |
| Initialization | सामान्यतः लूप से पहले लिखा जाता है। | सामान्यतः लूप के header में लिखा जाता है। |
| Updation | सामान्यतः लूप के अंदर लिखा जाता है। | सामान्यतः लूप के header में लिखा जाता है। |
| उपयोग | जब iterations की संख्या पहले से ज्ञात न हो। | जब iterations की संख्या पहले से ज्ञात हो। |
Infinite while Loop (अनंत व्हाइल लूप)
If the condition of a while loop always remains true, the loop continues indefinitely. Such a loop is called an infinite while loop.
यदि while loop की condition हमेशा true रहती है, तो लूप लगातार execute होता रहता है। ऐसे लूप को Infinite while Loop कहते हैं।
Example:
while (true){cout << "Hello ";}
This loop continuously prints Hello because its condition is always true. It can be terminated using an appropriate control mechanism, such as break.
यह लूप लगातार Hello print करता रहता है क्योंकि इसकी condition हमेशा true रहती है। इसे break जैसे control mechanism से terminate किया जा सकता है।
Comments
Post a Comment