do-while Loop in C Language with Examples

The do-while loop is an exit-controlled loop in C language. It is used to execute a block of statements repeatedly as long as the given condition is true. In a do-while loop, the condition is checked after executing the loop body. The initialization of a loop-control variable is generally performed before the loop. The statements inside the do block are executed first, and updation is generally performed inside the loop body. After that, the condition is checked using the while keyword. If the condition is true (non-zero), the loop body is executed again. If the condition is false (zero), the loop terminates and program control moves to the next statement after the loop.

C भाषा में do-while एक Exit-Controlled Loop है। इसका उपयोग किसी Statement या Statements के समूह को तब तक बार-बार निष्पादित करने के लिए किया जाता है, जब तक दी गई Condition True रहती है। do-while Loop में Condition की जाँच Loop Body निष्पादित होने के बाद की जाती है। Loop को नियंत्रित करने वाले Variable का Initialization सामान्यतः Loop से पहले किया जाता है। सबसे पहले do Block के अंदर लिखे गए Statements निष्पादित होते हैं और Updation सामान्यतः Loop Body के अंदर किया जाता है। इसके बाद while Keyword की सहायता से Condition की जाँच की जाती है। यदि Condition True (Non-zero) होती है, तो Loop Body दोबारा निष्पादित होती है। यदि Condition False (Zero) हो जाती है, तो Loop समाप्त हो जाता है और प्रोग्राम का कंट्रोल Loop के बाद वाले अगले Statement पर पहुँच जाता है।

The main advantage of a do-while loop is that its body executes at least once, even if the condition is false initially. It is useful in menu-driven programs, input validation, and situations where the statements must execute at least once. A semicolon (;) is required after the while(condition) part of a do-while loop. This is an important syntactical difference between while and do-while loops.

do-while Loop का मुख्य लाभ यह है कि इसकी Body कम से कम एक बार अवश्य निष्पादित होती है, भले ही शुरुआत में Condition False हो। इसका उपयोग Menu-Driven Programs, Input Validation तथा ऐसी परिस्थितियों में किया जाता है जहाँ Statements का कम से कम एक बार निष्पादित होना आवश्यक होता है। do-while Loop में while(condition) के बाद Semicolon (;) लगाना आवश्यक होता है। यह while और do-while Loops के बीच एक महत्वपूर्ण Syntactical Difference है।

Syntax of do-while Loop

initialization;
do
{
statements;
updation;
}
while (condition);

Explanation of Syntax:

  • Initialization: It assigns the initial value to the loop-control variable.

  • do: It starts the loop body.

  • Statements: These instructions are executed at least once.

  • Updation: It changes the value of the loop-control variable.

  • while(condition): It checks the condition after executing the loop body.

  • Semicolon (;): It is required after the while condition.

सिंटैक्स का विवरण:

  • Initialization: इसमें Loop-Control Variable को प्रारंभिक वैल्यू दी जाती है।

  • do: यह Loop Body को शुरू करता है।

  • Statements: ये Instructions कम से कम एक बार निष्पादित होते हैं।

  • Updation: इसमें Loop-Control Variable की वैल्यू बदली जाती है।

  • while(condition): यह Loop Body निष्पादित होने के बाद Condition की जाँच करता है।

  • Semicolon (;): While Condition के बाद Semicolon लगाना आवश्यक है।

Working of do-while Loop

The working of a do-while loop can be understood through the following steps:

  1. Initialization of the loop-control variable takes place.

  2. The do block is executed.

  3. The loop-control variable is updated.

  4. The condition is checked.

  5. If the condition is true, control returns to the do block.

  6. If the condition is false, the loop terminates.

do-while Loop की कार्यप्रणाली को निम्नलिखित चरणों से समझा जा सकता है:

  1. सबसे पहले Loop-Control Variable का Initialization किया जाता है।

  2. इसके बाद do Block निष्पादित होता है।

  3. Loop-Control Variable की वैल्यू Update की जाती है।

  4. Condition की जाँच की जाती है।

  5. यदि Condition True होती है, तो कंट्रोल वापस do Block पर पहुँचता है।

  6. यदि Condition False होती है, तो Loop समाप्त हो जाता है।

Example 1: C Program to Print Numbers from 1 to 10 Using do-while Loop

This program uses a do-while loop to display numbers from 1 to 10.

यह प्रोग्राम do-while Loop का उपयोग करके 1 से 10 तक की संख्याएँ प्रदर्शित करता है।

C Program:

#include <stdio.h>
int main(void)
{
int i = 1;
do
{
printf("%d ", i);
i++;
}
while (i <= 10);
return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10

Explanation:

Initially, the value of i is 1. The loop prints the value of i and increments it by 1. After executing the loop body, the condition i <= 10 is checked. The process continues until i becomes 11.

प्रारंभ में i की वैल्यू 1 होती है। Loop में i की वैल्यू प्रिंट की जाती है और इसे 1 से Increment किया जाता है। Loop Body निष्पादित होने के बाद i <= 10 Condition की जाँच होती है। यह प्रक्रिया तब तक चलती है जब तक i की वैल्यू 11 नहीं हो जाती।

Example 2: C Program to Demonstrate That do-while Executes at Least Once

This program demonstrates that a do-while loop executes its body at least once, even when the condition is false initially.

यह प्रोग्राम दर्शाता है कि do-while Loop की Condition शुरुआत में False होने पर भी इसकी Body कम से कम एक बार निष्पादित होती है।

C Program:

#include <stdio.h>
int main(void)
{
int i = 10;
do
{
printf("Value of i = %d\n", i);
i++;
}
while (i <= 5);
printf("Loop terminated.");
return 0;
}

Output:

Value of i = 10
Loop terminated.

Explanation:

The initial value of i is 10, so the condition i <= 5 is false. However, the do block executes once before the condition is checked. After that, the condition is found false and the loop terminates.

इस प्रोग्राम में i की प्रारंभिक वैल्यू 10 है, इसलिए i <= 5 Condition False होती है। लेकिन Condition की जाँच होने से पहले do Block एक बार निष्पादित होता है। इसके बाद Condition False मिलने पर Loop समाप्त हो जाता है।

Example 3: C Program to Print Even Numbers from 2 to 20 Using do-while Loop

This program displays even numbers from 2 to 20 using a do-while loop.

यह प्रोग्राम do-while Loop का उपयोग करके 2 से 20 तक की सम संख्याएँ प्रदर्शित करता है।

C Program:

#include <stdio.h>
int main(void)
{
int i = 2;
do
{
printf("%d ", i);
i = i + 2;
}
while (i <= 20);
return 0;
}

Output:

2 4 6 8 10 12 14 16 18 20

Explanation:

The variable i is initialized with 2. The program prints the value and increases it by 2 in each iteration. The loop continues while i is less than or equal to 20.

इस प्रोग्राम में Variable i को 2 से Initialize किया गया है। प्रोग्राम इसकी वैल्यू प्रिंट करता है और प्रत्येक Iteration में इसे 2 से बढ़ाता है। यह Loop तब तक चलता है जब तक i की वैल्यू 20 से कम या बराबर रहती है।

Example 4: Menu-Driven Calculator Using do-while Loop

A do-while loop is particularly useful in menu-driven programs because the menu should be displayed at least once. The user can select an operation and continue using the menu until the exit option is selected.

Menu-Driven Programs में do-while Loop विशेष रूप से उपयोगी होता है, क्योंकि Menu कम से कम एक बार प्रदर्शित होना चाहिए। यूजर अपनी आवश्यकता के अनुसार Operation चुन सकता है और Exit Option चुनने तक Menu का उपयोग जारी रख सकता है।

C Program:

#include <stdio.h>
int main(void)
{
int choice;
float a, b;
do
{
printf("\n----- MENU -----\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice >= 1 && choice <= 4)
{
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
}
switch (choice)
{
case 1:
printf("Result = %.2f\n", a + b);
break;
case 2:
printf("Result = %.2f\n", a - b);
break;
case 3:
printf("Result = %.2f\n", a * b);
break;
case 4:
if (b != 0)
printf("Result = %.2f\n", a / b);
else
printf("Division by zero is not allowed.\n");
break;
case 5:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
}
while (choice != 5);
return 0;
}

Sample Output:

----- MENU -----
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 1
Enter two numbers: 10 20
Result = 30.00
----- MENU -----
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 5
Exiting program...

Explanation:

The menu is displayed inside the do block. The user selects an operation, and the switch statement performs the selected calculation. The loop continues as long as choice != 5. When the user selects 5, the loop terminates.

इस प्रोग्राम में Menu को do Block के अंदर प्रदर्शित किया जाता है। यूजर किसी Operation का चयन करता है और switch Statement के द्वारा चुनी गई Calculation की जाती है। यह Loop तब तक चलता है जब तक choice != 5 रहता है। जब यूजर 5 चुनता है, तब Loop समाप्त हो जाता है।

Important Points About do-while Loop

  1. The do-while loop is an exit-controlled loop because its condition is checked after executing the loop body.
    do-while एक Exit-Controlled Loop है, क्योंकि इसमें Loop Body निष्पादित होने के बाद Condition की जाँच की जाती है।

  2. The loop body executes at least once, regardless of whether the condition is initially true or false.
    Condition शुरुआत में True हो या False, Loop Body कम से कम एक बार निष्पादित होती है।

  3. Initialization is generally performed before the loop.
    Initialization सामान्यतः Loop से पहले किया जाता है।

  4. Updation is generally performed inside the loop body.
    Updation सामान्यतः Loop Body के अंदर किया जाता है।

  5. The while(condition) part must end with a semicolon (;).
    while(condition) के बाद Semicolon (;) लगाना आवश्यक है।

  6. If the condition remains true indefinitely, an infinite loop may occur.
    यदि Condition लगातार True रहती है, तो Infinite Loop बन सकता है।

  7. The do-while loop is useful for menu-driven programs and input validation.
    do-while Loop Menu-Driven Programs और Input Validation के लिए उपयोगी है।

Difference Between while and do-while Loop

while Loopdo-while Loop
It is an entry-controlled loop.It is an exit-controlled loop.
Condition is checked before executing the loop body.Condition is checked after executing the loop body.
It may execute zero times.It executes at least once.
The loop body may not execute if the initial condition is false.The loop body executes once even if the initial condition is false.
No semicolon is required after the loop condition.A semicolon is required after the while condition.

while Loop
do-while Loop
यह Entry-Controlled Loop है।यह Exit-Controlled Loop है।
इसमें Loop Body से पहले Condition की जाँच होती है।इसमें Loop Body के बाद Condition की जाँच होती है।
यह शून्य बार भी निष्पादित हो सकता है।यह कम से कम एक बार निष्पादित होता है।
यदि शुरुआती Condition False हो, तो Loop Body निष्पादित नहीं होती है।शुरुआती Condition False होने पर भी Loop Body एक बार निष्पादित होती है।
Loop Condition के बाद Semicolon आवश्यक नहीं होता है।While Condition के बाद Semicolon आवश्यक होता है।

The do-while loop is useful when a program needs to execute a block of statements at least once before checking a condition. It is commonly used in menu-driven programs, input validation, and interactive applications.

do-while Loop तब उपयोगी होता है जब किसी प्रोग्राम में Condition की जाँच से पहले Statements के समूह को कम से कम एक बार निष्पादित करना आवश्यक हो। इसका उपयोग Menu-Driven Programs, Input Validation और Interactive Applications में किया जाता है.

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025