C Programming में while लूप एक बहुत ही लोकप्रिय और ज़्यादा इस्तेमाल किया जाने वाला लूप है। यह एक control flow statement है, जिसका उपयोग किसी दी गई शर्त (condition) के आधार पर किसी कोड ब्लॉक को बार-बार चलाने के लिए किया जाता है।
इस पोस्ट में हम जानेंगे कि while लूप क्या होता है, इसका उपयोग कैसे किया जाता है, साथ ही इसका सिंटैक्स और उदाहरण प्रोग्राम भी देखेंगे।
Table of Contents
What is the While Loop in C Programming ?
while लूप एक ऐसा लूप है जो सबसे पहले शर्त (condition) को चेक करता है। अगर शर्त सही होती है, तो लूप के अन्दर का कोड execute होता है।
C Programming में while लूप का इस्तेमाल तब किया जाता है जब हमें किसी कोड को बार-बार चलाना हो, जब तक कि दी हुई शर्त सही रहती है।
क्योंकि यह एक Entry controlled लूप है, इसलिए हर बार लूप चलने से पहले शर्त को चेक किया जाता है।
Syntax of While Loop in C Programming
while (condition) {
// code to be executed repeatedly
}
How to Work While Loop in c Programming
चलिए एक program बनाके समझते हैं। कैसे c programming में while loop काम करता है।
1 to N number Print program
#include <stdio.h>
int main() {
int i = 1, n;
printf("Enter a number: ");
scanf("%d", &n);
while(i <= n) {
printf("%d\n", i);
i++;
}
return 0;
}
Output:
Enter a number: 3
1
2
3
Step by Step Working:
- Condition Check → सबसे पहले चेक होगा i <= n.
- अगर condition true है → loop के अन्दर का code चलेगा।
- अगर condition false है → loop वहीं रुक जाएगा।
- Code Execution → अगर condition true है तो printf(“%d\n”, i); चलेगा और i की value print होगी।
- Updation (i++) → इसके बाद i++ होगा यानी i की value 1 बढ़ जाएगी।
- Repeat → फिर से condition check होगी (i <= n)।
- जब तक condition true है, loop बार-बार चलता रहेगा।
- जैसे ही condition false हो जाएगी, loop बंद हो जाएगा।
FlowChart of while loop in c programming

While Loop in C Programming Example Code
1. Bus Ticket Booking program
यह एक ticket booking program है। 10 seats available हैं, जब तक seats खत्म नहीं होती, user टिकिट book करता रहेगा।
#include <stdio.h>
int main()
{
int seats = 10;
int booked = 0;
while (booked < seats)
{
char choice;
printf("Seat available! Do you want to book? (y/n): ");
scanf(" %c", &choice);
if (choice == 'y' || choice == 'Y')
{
booked++;
printf("\nEnter Your Name: ");
char name[50];
scanf(" %s", name);
printf("Ticket booked for %s.\n", name);
printf(" Seat booked! Total booked: %d\n", booked);
}
else
{
printf("Booking skipped.\n");
if (choice == 'n' || choice == 'N')
{
printf("You chose not to book a seat.\n");
}
else
{
printf("Invalid choice. Please enter 'y' or 'n'.\n");
}
}
}
printf("\nAll seats booked! No more tickets available.\n");
return 0;
}
Output:
Seat available! Do you want to book? (y/n): y
Enter Your Name: Anwar
Ticket booked for Anwar.
Seat booked! Total booked: 1
Seat available! Do you want to book? (y/n):
2. Sum of Digits using while loop
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while(num > 0) {
sum = sum + (num % 10); // last digit निकालकर add कर दो
num = num / 10; // last digit हटा दो
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Output:
Enter a number: 123456789
Sum of digits = 45
Nested While Loop in C Programming
जब एक loop को दूसरे लूप के अंदर लिखा जाता है तो उसे Nested loop कहा जाता है। जब हमें एक loop के अंदर दूसरा loop चलाना होता है।
मतलब, किसी काम को बार-बार दोहराना है, और हर बार उसमें एक और छोटा repetition भी करना होता है तब nested while loop in c programming का उपयोग करते है।
Example Program to Print Multiplication Table
#include <stdio.h>
int main() {
int start, end;
int i, j;
// User inputs
printf("Enter the starting number of table: ");
scanf("%d", &start);
printf("Enter the ending number of table: ");
scanf("%d", &end);
i = start; // outer loop ka counter
// Outer loop -> table number control
while(i <= end) {
printf("\nMultiplication Table of %d:\n", i);
j = 1; // inner loop ka counter
// Inner loop -> har table ka multiplication
while(j <= 10) {
printf("%d x %d = %d\n", i, j, i * j);
j++;
}
i++;
}
return 0;
}
Output:
Enter the starting number of table: 2
Enter the ending number of table: 4
Multiplication Table of 2:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Multiplication Table of 3:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Multiplication Table of 4:
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Explanation :
1. User Input
- Program jab shuru hoga tab user start number and end number enter karne ke liye ke liye kaha jayega:
- start → किस number से table शुरू करना है
- end → किस number तक table print करना है
Example: अगर start = 2 और end = 4, तो program 2 से 4 तक की tables print करेगा।
2. Outer While Loop (Table Number Control)
i = start;
while(i <= end) {
…
i++;
}
- यह loop i को control करता है।
- i represent करता है किस number की table चल रही है।
- जब तक i <= end, outer loop चलता रहेगा।
- हर बार outer loop खत्म होने पर i++ होता है, यानी next table पर यह jump तब तक करता रहेगा जब तक कि table अंतिम नंबर तक नहीं पहुंच जाता।
Example:
पहले i = 2 (table of 2) → फिर i = 3 (table of 3) → फिर i = 4 (table of 4)।
3. Inner While Loop (Multiplication Print)
j = 1;
while(j <= 10) {
printf(“%d x %d = %d\n”, i, j, i * j);
j++;
}
- यह loop हर table के अंदर चलता है।
- j represent करता है multiplier (1 से 10 तक)।
- जब तक j <= 10, inner loop चलता है और i * j का result print करता है।
- हर बार j++ करके अगले step पर जाता है।
Example (जब i = 2):
2 x 1 = 2
2 x 2 = 4
…
2 x 10 = 20
4. Output Flow
- पहले outer loop table का number set करता है।
- फिर inner loop उस table की पूरी multiplication print करता है।
- Inner loop खत्म होने पर outer loop अगली table पर चला जाता है।
इस तरह nested loop मिलकर multiple tables print करते हैं।
Infinite While Loop in C Programming
Infinite Loop का मतलब है ऐसा loop जो कभी रुकता ही नहीं है। यानी उसकी condition हमेशा true रहती है, इसलिए program बार-बार वही instructions execute करता रहता है।
Example (While Loop in C Programming)
#include <stdio.h>
int main() {
while(1) { // condition हमेशा true
printf("AnwarCodes.com\n");
}
return 0;
}
Output:
AnwarCodes.com
AnwarCodes.com
AnwarCodes.com……
यहाँ while(1) का मतलब है “जब तक condition true है loop चलता रहे”।
लेकिन 1 हमेशा true होता है, इसलिए ये loop कभी खत्म नहीं होगा।
Result: Screen पर लगातार “Hello World” print होता रहेगा, जब तक आप program को manually Ctrl + C press करके न रोक दें।
Common Causes of Infinite Loop
Condition कभी false न होना।
int i = 1;
while(i <= 5) {
printf("%d\n", i);
// i++ करना भूल गए → i हमेशा 1 रहेगा
}
इस case में loop 1 print करता रहेगा क्योंकि i को Update नहीं किया जा रहा है।
Wrong condition
int i = 10;
while(i > 0) {
printf("%d\n", i);
i++; // यहां decrement की अवश्यसकता है , increment कर दिया गया।
}
i हमेशा बढ़ेगा → condition हमेशा true रहेगी।
While Loop vs do While Loop in C Programming
C programming में while loop और do-while loop दोनों looping statements हैं, लेकिन इन दोनों के बीच कुछ important differences हैं। निचे दिए गए टेबल में देख सकते है
- While Loop: जब हमें पता नहीं होता कि loop कितनी बार चलेगा (जैसे user input लेना, file पढ़ना)
- Do-While Loop: जब हमें चाहिए कि code कम से कम एक बार चले ही (जैसे menu-driven programs, ATM pin validation)।
Comparison Table
Feature | While Loop | Do-While Loop |
Condition Check | पहले | बाद में |
Minimum Execution | 0 बार भी हो सकता है | कम से कम 1 बार ज़रूर होता है |
Syntax Ending | } पर ख़त्म होता है | } while(condition); पर ख़त्म |
Use Cases | Data reading, unknown loops | Menu-driven, mandatory run |
Quizzes for while loop in c programming
Q1. इस प्रोग्राम का आउटपुट क्या होगा?
Quiz 2: While loop का सही सिंटैक्स चुनें:
Quiz 3: यह प्रोग्राम क्या आउटपुट देगा?
Quiz 4: निम्न में से कौन-सा infinite loop है?
Quiz 5: यह लूप कितनी बार चलेगा?
int i = 1;
while (i < 3) {
i++;
}
अब आपकी बारी है quzzes का Answer कमेंट box में लिखे फिर Answer देखो बटन को press करके देखें।
FAQs – while loop in c programming
Ans: While loop एक control statement है जो condition true रहने तक code block को बार-बार चलाता है।
Ans: While loop पहले condition check करता है, जबकि Do-While loop code को कम से कम एक बार चलाता है।
while(condition) {
// code
}
Ans: अगर condition हमेशा true रहे (जैसे while(1)
), तो loop infinite हो जाएगा।
Ans: Menu driven programs, user input लेने और file पढ़ने में while loop का सबसे ज़्यादा use होता है।