Do while loop in C programming में एक looping statement है, बिलकुल वैसे ही जैसे पिछले ब्लॉग में हमने for loop देखा था। इस पोस्ट में हम जानेंगे कि do while loop C programming में कैसे काम करता है, कहाँ उपयोग होता है, और इसके real-life examples भी देखेंगे। Do while loop को step-by-step समझने के लिए इस पूरे ब्लॉग को पढ़ें।
Table of Contents
What is Do While Loop in C?
Programming में जब हमें किसी particular block of code को बार-बार execute करना होता है, तब हम loops का उपयोग करते हैं।
C programming में 3 types के loops होते हैं:
- for loop
- while loop
- do while loop
हर loop का काम एक जैसा होता है (code repetition), लेकिन उनका execution method और use case अलग होता है।
Loop execution हमेशा condition (true या false) पर depend करता है:
- अगर condition true होती है → loop body execute होती है।
- अगर condition false होती है → loop body execute नहीं होती।
लेकिन do while loop in C programming थोड़ा special है। इसमें अगर condition false भी हो, तब भी loop की body कम से कम एक बार execute होती है।
इसलिए जब हमें यह ensure करना हो कि code block एक बार तो ज़रूर चले, तो हम do while loop का उपयोग करते हैं।
Do While Loop in C – Syntax
do {
// Code to be executed
} while (condition);
Syntax Explanation:
- do block के अंदर लिखा गया code सबसे पहले एक बार execute होता है।
- इसके बाद condition check होती है।
- अगर condition true है → loop फिर से चलेगा।
- अगर condition false है → loop terminate हो जाएगा।
Example Program – Do While Loop in C
#include <stdio.h>
int main() {
int i = 1;
// Do While Loop Example
do {
printf("Welcome to AnwarCodes.com\n");
i++;
} while (i <= 5);
return 0;
}
Output:
Welcome to AnwarCodes.com
Welcome to AnwarCodes.com
Welcome to AnwarCodes.com
Welcome to AnwarCodes.com
Welcome to AnwarCodes.com
Flowchart of Do While Loop in C

Real Life Example – ATM PIN Verification
#include <stdio.h>
int main() {
int pin, correctPin = 1234;
int attempts = 0;
do {
printf("Enter your 4-digit PIN: ");
scanf("%d", &pin);
attempts++;
if (pin == correctPin) {
printf("Access Granted! Welcome to your account.\n");
break;
} else {
printf("Incorrect PIN. Try again.\n");
}
} while (attempts < 3);
if (pin != correctPin) {
printf("Your card is blocked due to 3 incorrect attempts.\n");
}
return 0;
}
Output:
Enter your 4-digit PIN: 4321
Incorrect PIN. Try again.
Enter your 4-digit PIN: 7865
Incorrect PIN. Try again.
Enter your 4-digit PIN: 5555
Incorrect PIN. Try again.
Your card is blocked due to 3 incorrect attempts.
Enter your 4-digit PIN: 1234
Access Granted! Welcome to your account.
Nested Do While Loop in C
C programming में अगर एक do while loop के अंदर दूसरा loop हो तो उसे nested do while loop कहते हैं।
Example – Multiplication Tables Generator
#include <stdio.h>
int main() {
int number = 1, table, i;
// Outer loop → multiple tables print करेगा
do {
printf("\nEnter a number to print its table (0 to exit): ");
scanf("%d", &table);
if (table == 0) {
break; // Exit if user enters 0
}
i = 1;
printf("\nTable of %d:\n", table);
// Inner loop → single table print करेगा
do {
printf("%d x %d = %d\n", table, i, table * i);
i++;
} while (i <= 10);
number++;
} while (1); // Infinite until user exits
printf("\nThanks for using Multiplication Table Generator \n");
return 0;
}
Output:
Enter a number to print its table (0 to exit): 8
Table of 8:
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
Advantages of Do While Loop in C
- कम से कम एक बार execute होना guaranteed है।
- User input लेने में perfect (जैसे PIN या password verify करना)।
- Simple syntax, beginner friendly।
- Menu-driven programs (ATM, calculator, game menus) में useful।
Disadvantages of Do While Loop in C
- Extra execution risk (condition false होने पर भी एक बार चलेगा)।
- Readability issue (condition नीचे होने से beginners confuse हो सकते हैं)।
- Limited use cases – हर जगह fit नहीं होता।
- Debugging मुश्किल हो सकती है अगर condition गलत लिखी गई हो।
Do While Loop in C – Quizzes
Quiz 1: Do while loop में code block कम से कम कितनी बार execute होता है?
Quiz 2: C भाषा में do while loop का सही syntax कौन सा है?
Quiz 3: While loop और Do while loop में क्या अंतर है?
Quiz 4: Do while loop का सबसे ज़्यादा उपयोग कहाँ होता है?
Quiz 5: नीचे दिए गए प्रोग्राम का आउटपुट क्या होगा?
int i = 5;
do {
printf("%d ", i);
i++;
} while(i < 5);
FAQS
Q 1: Do while loop का उपयोग कब करना चाहिए?
Ans. जब हमें कम से कम एक बार code चलाना हो, चाहे condition false ही क्यों न हो।
Que 2: Do while loop में semicolon (;) जरूरी है क्या?
Ans→ हाँ, while(condition); के अंत में semicolon लगाना जरूरी है, वरना error आएगा।
Que 3: क्या C भाषा में nested do while loop बना सकते हैं?
Ans → हाँ, एक do while loop के अंदर दूसरा do while loop लिखा जा सकता है।
Que 4: Do while loop की speed while loop से ज्यादा है क्या?
Ans→ नहीं, दोनों की speed लगभग same है। फर्क सिर्फ condition check करने के order का है।
Que 5: अगर condition हमेशा true हो तो क्या होगा?
Ans→ Loop अनंत बार (infinite loop) चलता रहेगा जब तक break statement का इस्तेमाल न किया जाए।
Conclusion
इस ब्लॉग में आपने step-by-step सीखा कि do while loop in C कैसे काम करता है, उसका syntax, examples, real-life उपयोग, advantages, disadvantages और FAQs।
Do while loop beginner programmers के लिए आसान और उपयोगी है, खासकर जब हमें कम से कम एक बार code block execute कराना हो।
अब आपकी बारी है – खुद से छोटे-छोटे programs बनाकर practice करें, जैसे menu-driven calculator या guess the number game। Practice से ही loop concepts clear होंगे।