Introduction to While Loop in Hindi
/ BCA / Programming with C and CPP
While Loop Explained with Real Examples in Hindi
Introduction to While Loop in Hindi
जब हम किसी काम को बार-बार दोहराना चाहते हैं, जैसे कि किसी student की attendance check करना, या किसी customer की repeated entry लेना, तो हमें बार-बार वही code लिखने की जरूरत नहीं होती। इसके लिए हम loop का उपयोग करते हैं। While Loop एक ऐसा loop है जो तब तक चलता रहता है जब तक कोई condition true रहती है। जैसे ही condition false होती है, loop रुक जाता है।
While Loop को हम Entry-Controlled Loop भी कहते हैं, क्योंकि इसमें पहले condition check की जाती है, फिर अंदर के statements execute होते हैं। यह सबसे basic और frequently used loop है C, C++ और अन्य programming languages में।
Use Case:
- Bank में customer की repeated entry लेना
- Students के marks input करना जब तक input valid हो
- Password check करते रहना जब तक सही न हो
Syntax and Flow of While Loop in Hindi
While Loop का syntax बहुत simple होता है। सबसे पहले condition दी जाती है, और जब तक वो condition true रहती है, loop चलता रहता है।
Syntax:
while(condition) {
// statements
}
ऊपर दिए गए syntax में:
- while एक keyword है
- condition एक logical expression होता है जो true या false return करता है
- जब condition true रहती है, तब curly braces {} के अंदर का code execute होता है
Flowchart:
Flowchart में सबसे पहले condition check होती है। अगर वो true होती है तो body of loop execute होता है, फिर से वही condition check होती है। जैसे ही condition false हो जाती है, program loop से बाहर आ जाता है।
Example:
मान लीजिए हमें 1 से 5 तक के numbers print करने हैं:
#include <stdio.h>
int main() {
int i = 1;
while(i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Entry-Controlled Condition in While Loop in Hindi
Entry-Controlled Loop का मतलब है कि loop में entry लेने से पहले ही condition check की जाती है। While Loop इसी category में आता है। अगर पहली बार में ही condition false हो जाती है, तो loop का body एक बार भी execute नहीं होता।
Example:
नीचे के उदाहरण में loop एक बार भी execute नहीं होगा क्योंकि condition शुरू से ही false है:
#include <stdio.h>
int main() {
int i = 10;
while(i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Output: कुछ भी नहीं (loop नहीं चलेगा)
Entry-Controlled Loop के लाभ:
- Unnecessary execution से बचाता है
- Invalid input या unexpected behavior को रोकता है
- Control flow clear और predictable रहता है
While Loop Use in Real-Life Programs in Hindi
While Loop का real life में बहुत सारे उपयोग होते हैं। जब तक कोई user correct password नहीं डालता, तब तक उसे बार-बार try करने देना, या किसी menu को तब तक दिखाना जब तक user 'Exit' न करे - ये सभी जगह while loop का use किया जाता है।
Use Case 1: Password Check System
#include <stdio.h>
#include <string.h>
int main() {
char password[20];
while(strcmp(password, "1234") != 0) {
printf("Enter Password: ");
scanf("%s", password);
}
printf("Access Granted\n");
return 0;
}
Use Case 2: Menu Driven Program
#include <stdio.h>
int main() {
int choice = 0;
while(choice != 4) {
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
}
printf("Exited from Program\n");
return 0;
}
Use Case 3: ATM Simulation
मान लीजिए user को तब तक money withdraw करने देना है जब तक उसकी balance 0 न हो जाए। हर बार withdrawal के बाद balance चेक किया जाएगा।
#include <stdio.h>
int main() {
int balance = 1000, withdraw;
while(balance > 0) {
printf("Enter amount to withdraw: ");
scanf("%d", &withdraw);
if(withdraw <= balance) {
balance -= withdraw;
printf("Remaining Balance: %d\n", balance);
} else {
printf("Insufficient Balance\n");
}
}
printf("Balance Zero - No further transactions allowed.\n");
return 0;
}
Advantages of While Loop:
- Condition check पहले होता है जिससे safety बढ़ती है
- Unknown iterations वाले cases के लिए perfect होता है
- Simple logic और easy implementation
Where to Use While Loop:
- जब iterations का count पहले से पता न हो
- Input validation में
- Real-time systems जैसे ATM, Login Form, etc. में
FAQs
while(condition) {
// statements
} इसमें condition एक logical expression होता है और statements तब तक repeat होते हैं जब तक condition true रहती है।
Do-While Loop में पहले statement execute होता है फिर condition check होती है (exit-controlled), यानी minimum एक बार loop जरूर चलता है।