while & do-while Loop
while और do-while Loop
while Loop
The while loop repeats a block of code as long as its condition is true. Use it when you don't know in advance how many times the loop should run.
while (condition) { // body // update variable (to avoid infinite loop!) }
The condition is checked before each iteration. If the condition is false from the start, the body never executes.
Example 1: Print 1 to 5
#include <stdio.h> int main() { int i = 1; // Initialization (outside loop) while (i <= 5) { // Condition printf("%d\\n", i); i++; // Update (inside loop) } return 0; }
Example 2: Sum until User Enters 0
#include <stdio.h> int main() { int num, sum = 0; printf("Enter numbers (0 to stop):\\n"); scanf("%d", &num); while (num != 0) { sum += num; scanf("%d", &num); } printf("Total sum = %d\\n", sum); return 0; }
Example 3: Reverse Digits of a Number
#include <stdio.h> int main() { int num, rev = 0; printf("Enter number: "); scanf("%d", &num); while (num != 0) { int digit = num % 10; rev = rev * 10 + digit; num /= 10; } printf("Reversed: %d\\n", rev); return 0; }
do-while Loop
The do-while loop is similar to while, but the body executes at least once because the condition is checked after the body.
do { // body (runs at least ONCE) } while (condition); // Note: semicolon here!
Example: Menu-Driven Program
#include <stdio.h> int main() { int choice; do { printf("\\n1. Add\\n2. Subtract\\n3. Exit\\nEnter choice: "); scanf("%d", &choice); if (choice == 1) printf("Adding...\\n"); else if (choice == 2) printf("Subtracting...\\n"); else if (choice == 3) printf("Goodbye!\\n"); else printf("Invalid choice!\\n"); } while (choice != 3); return 0; }
The do-while loop requires a semicolon after the closing while (condition);. Forgetting it causes a compile error.
while vs do-while vs for
| Feature | for | while | do-while |
|---|---|---|---|
| Condition check | Before (entry-controlled) | Before (entry-controlled) | After (exit-controlled) |
| Min executions | 0 (if condition false) | 0 (if condition false) | 1 (always) |
| Best used when | Count known | Count unknown | Must run at least once |
| Update location | In for() header | Inside body | Inside body |
Infinite Loop
A loop that never ends. Use break to exit from inside.
while (1) { // Always true int x; scanf("%d", &x); if (x == 0) break; // Exit when 0 entered printf("You entered: %d\\n", x); }
Summary
while— condition checked before body; body may never rundo-while— body runs first, condition checked after; runs at least oncedo-whileneeds semicolon:} while (cond);- Use
whilefor unknown count,forfor known count - Always update the loop variable to avoid infinite loops
while Loop
while loop तब तक code block repeat करता है जब तक उसकी condition true है। इसे use करते हैं जब पहले से नहीं पता कि loop कितनी बार चलेगा।
while (condition) { // body // variable update (infinite loop से बचने के लिए!) }
Condition पहले check होती है। अगर शुरू से false है तो body कभी execute नहीं होती।
int i = 1; while (i <= 5) { printf("%d\\n", i); i++; }
Number Reverse करना
int num = 12345, rev = 0; while (num != 0) { rev = rev * 10 + num % 10; num /= 10; } printf("Reverse: %d\\n", rev);
do-while Loop
do-while while जैसा है, लेकिन body कम से कम एक बार execute होती है क्योंकि condition body के बाद check होती है।
do { // body (कम से कम एक बार चलेगा) } while (condition); // यहाँ semicolon ज़रूरी है!
int choice; do { printf("1. Jod\\n2. Ghatao\\n3. Exit\\nChoice: "); scanf("%d", &choice); if (choice == 3) printf("Alvida!\\n"); } while (choice != 3);
while vs do-while vs for में अंतर
| Feature | for | while | do-while |
|---|---|---|---|
| Condition check | पहले | पहले | बाद में |
| Minimum executions | 0 बार | 0 बार | 1 बार हमेशा |
| कब use करें | Count पता हो | Count नहीं पता | एक बार ज़रूर चलना हो |
सारांश
while— condition पहले check होती है; body शायद कभी न चलेdo-while— body पहले चलती है, condition बाद में; कम से कम एक बार चलती हैdo-whileमें semicolon ज़रूरी:} while (cond);- Infinite loop से बचने के लिए loop variable update करना न भूलें