do-while Loop in C++ – Syntax, Working and Examples with Output C++ में do-while लूप – सिंटैक्स एवं उदाहरण
A do-while loop is an exit-controlled loop in C++. It is used to execute a block of statements repeatedly as long as the given condition is true. Unlike for and while loops, the condition of a do-while loop is checked after executing the loop body. Therefore, the statements inside the loop execute at least once, even if the condition is false initially.
C++ में do-while loop एक Exit-Controlled Loop है। इसका उपयोग किसी statement या statements के समूह को तब तक बार-बार execute करने के लिए किया जाता है, जब तक दी गई condition सत्य होती है। for और while loops के विपरीत, इसमें condition को loop body execute होने के बाद check किया जाता है। इसलिए condition प्रारंभ में false होने पर भी लूप के statements कम से कम एक बार execute होते हैं।
Syntax of do-while Loop (do-while लूप का सिंटैक्स)
initialization;do{statements;updation;}while (condition);
Parts of do-while loop (do-while लूप के भाग):
- Initialization: It initializes the loop control variable before the loop starts.Initialization: इसमें लूप शुरू होने से पहले loop control variable को प्रारंभिक value दी जाती है।
- do Block: It contains the statements that are executed at least once.do Block: इसमें वे statements होते हैं जो कम से कम एक बार execute होते हैं।
- Updation: It changes the value of the loop control variable after executing the statements.Updation: इसमें statements execute होने के बाद loop control variable की value बदली जाती है।
- Condition: It is checked after executing the loop body. If the condition is true, the loop repeats; otherwise, it terminates.Condition: इसे loop body execute होने के बाद check किया जाता है। Condition true होने पर लूप दोबारा execute होता है, अन्यथा समाप्त हो जाता है।
Note: A semicolon (;) is required after the while(condition) in a do-while loop.
ध्यान दें: do-while loop में while(condition) के बाद semicolon (;) लगाना आवश्यक है।
Working of do-while Loop (do-while लूप की कार्यप्रणाली)
The execution of a do-while loop follows these steps:
do-while loop का execution निम्नलिखित चरणों में होता है:
Initialization is performed before the loop.
The statements inside the
doblock are executed.The updation statement is executed.
The condition is checked.
If the condition is true, the loop executes again. Otherwise, the loop terminates.
सबसे पहले Initialization किया जाता है।
इसके बाद
doblock के statements execute होते हैं।फिर Updation statement execute होता है।
इसके बाद Condition check होती है।
यदि Condition true है, तो लूप दोबारा execute होता है, अन्यथा समाप्त हो जाता है।
Example 1: C++ Program to Print the Table of a Number Using do-while Loop
इस उदाहरण में do-while loop का उपयोग करके किसी संख्या का पहाड़ा 1 से 10 तक print किया गया है।
#include <iostream>using namespace std;int main(){int i = 1, n;cout << "Enter a Number: ";cin >> n;cout << "Table of " << n << endl;do{cout << n << " x " << i << " = " << n * i << endl;i++;}while (i <= 10);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 (व्याख्या):
int i = 1initializes the loop control variable.The
doblock prints the multiplication table.i++increases the value ofiby 1 after each iteration.i <= 10checks whether the loop should continue.int i = 1लूप control variable को initialize करता है।doblock पहाड़ा print करता है।i++प्रत्येक iteration के बादiकी value को 1 बढ़ाता है।i <= 10check करता है कि लूप continue होना चाहिए या नहीं।
Example 2: C++ Program for Counting from 1 to 100 Using do-while Loop
इस उदाहरण में do-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;do{cout << setw(3) << i << " ";if (i % 10 == 0){cout << endl;}i++;}while (i <= 100);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.doblock prints the current value ofi.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.i <= 100checks the loop condition after each iteration.int i = 1counting variable को initialize करता है।doblock मेंiकी वर्तमान value print होती है।setw(3)अगले output value के लिए minimum field width 3 characters निर्धारित करता है।i % 10 == 0check करता है कि वर्तमान संख्या 10 से divisible है या नहीं। यदि condition true होती है, तो अगली संख्या नई line में print होती है।i++counting variable की value को 1 बढ़ाता है।i <= 100प्रत्येक iteration के बाद loop condition check करता है।
Example 3: Demonstration of do-while Loop Executing at Least Once
The following program demonstrates that a do-while loop executes at least once, even when its condition is initially false.
निम्न प्रोग्राम यह प्रदर्शित करता है कि do-while loop की condition प्रारंभ में false होने पर भी इसके statements कम से कम एक बार execute होते हैं।
#include <iostream>using namespace std;int main(){int i = 10;do{cout << "Value of i = " << i << endl;i++;}while (i < 5);return 0;}
Output:
Value of i = 10Explanation (व्याख्या):
Here, the condition i < 5 is false because i is initially 10. However, the do block executes once before the condition is checked.
यहाँ i < 5 condition false है क्योंकि i की प्रारंभिक value 10 है। फिर भी condition check होने से पहले do block एक बार execute होता है।
Difference Between while and do-while Loop (while और do-while लूप में अंतर)
| Basis | while Loop | do-while Loop |
|---|---|---|
| Control | Entry-controlled loop | Exit-controlled loop |
| Condition | Checked before the loop body. | Checked after the loop body. |
| Execution | May execute zero times. | Executes at least once. |
| Syntax | Condition is written after while. | Condition is written after the do block. |
| Semicolon | No semicolon is required after the condition. | Semicolon is required after while(condition). |
| Use | Useful when execution depends on a condition checked first. | Useful when the statements must execute at least once. |
आधार | while Loop | do-while Loop |
|---|---|---|
| नियंत्रण | Entry-controlled loop | Exit-controlled loop |
| Condition | Loop body से पहले check होती है। | Loop body के बाद check होती है। |
| Execution | शून्य बार भी execute हो सकता है। | कम से कम एक बार execute होता है। |
| Syntax | Condition, while के बाद लिखी जाती है। | Condition, do block के बाद लिखी जाती है। |
| Semicolon | Condition के बाद semicolon आवश्यक नहीं है। | while(condition) के बाद semicolon आवश्यक है। |
| उपयोग | जब पहले condition check करना आवश्यक हो। | जब statements को कम से कम एक बार execute करना हो। |
The do-while loop is useful when a block of statements must execute at least once before checking a condition. It is commonly used in menu-driven programs, input validation, and applications that require repeated user interaction.
do-while loop तब उपयोगी होता है जब किसी condition को check करने से पहले statements को कम से कम एक बार execute करना आवश्यक हो। इसका उपयोग सामान्यतः menu-driven programs, input validation तथा user interaction वाले applications में किया जाता है.
Comments
Post a Comment