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 लूप के भाग):

  1. Initialization: It initializes the loop control variable before the loop starts.
    Initialization: इसमें लूप शुरू होने से पहले loop control variable को प्रारंभिक value दी जाती है।

  2. do Block: It contains the statements that are executed at least once.
    do Block: इसमें वे statements होते हैं जो कम से कम एक बार execute होते हैं।

  3. Updation: It changes the value of the loop control variable after executing the statements.
    Updation: इसमें statements execute होने के बाद loop control variable की value बदली जाती है।

  4. 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 do block 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 किया जाता है।

  • इसके बाद do block के 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: 5
Table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Explanation (व्याख्या):

  • int i = 1 initializes the loop control variable.

  • The do block prints the multiplication table.

  • i++ increases the value of i by 1 after each iteration.

  • i <= 10 checks whether the loop should continue.

  • int i = 1 लूप control variable को initialize करता है।

  • do block पहाड़ा print करता है।

  • i++ प्रत्येक iteration के बाद i की value को 1 बढ़ाता है।

  • i <= 10 check करता है कि लूप 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 100
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

Explanation (व्याख्या):

  • int i = 1 initializes the counting variable.

  • do block prints the current value of i.

  • setw(3) sets the minimum field width to 3 characters for the next output value.

  • i % 10 == 0 checks 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 <= 100 checks the loop condition after each iteration.

  • int i = 1 counting variable को initialize करता है।

  • do block में i की वर्तमान value print होती है।

  • setw(3) अगले output value के लिए minimum field width 3 characters निर्धारित करता है।

  • i % 10 == 0 check करता है कि वर्तमान संख्या 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 = 10

Explanation (व्याख्या):

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 लूप में अंतर)

Basiswhile Loopdo-while Loop
ControlEntry-controlled loopExit-controlled loop
ConditionChecked before the loop body.Checked after the loop body.
ExecutionMay execute zero times.Executes at least once.
SyntaxCondition is written after while.Condition is written after the do block.
SemicolonNo semicolon is required after the condition.Semicolon is required after while(condition).
UseUseful when execution depends on a condition checked first.Useful when the statements must execute at least once.

आधार

while Loop

do-while Loop
नियंत्रणEntry-controlled loopExit-controlled loop
ConditionLoop body से पहले check होती है।Loop body के बाद check होती है।
Executionशून्य बार भी execute हो सकता है।कम से कम एक बार execute होता है।
SyntaxCondition, while के बाद लिखी जाती है।Condition, do block के बाद लिखी जाती है।
SemicolonCondition के बाद 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

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)