🟢 Beginner  ·  Lesson 14

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.

Syntax
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

C Language
#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;
}
1 2 3 4 5

Example 2: Sum until User Enters 0

C Language
#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;
}
Enter numbers (0 to stop): 5 10 3 7 0 Total sum = 25

Example 3: Reverse Digits of a Number

C Language
#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;
}
Enter number: 12345 Reversed: 54321

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.

Syntax
do {
    // body (runs at least ONCE)
} while (condition);  // Note: semicolon here!

Example: Menu-Driven Program

C Language – Menu with do-while
#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;
}
⚠️ Don't forget the semicolon!

The do-while loop requires a semicolon after the closing while (condition);. Forgetting it causes a compile error.

while vs do-while vs for

Featureforwhiledo-while
Condition checkBefore (entry-controlled)Before (entry-controlled)After (exit-controlled)
Min executions0 (if condition false)0 (if condition false)1 (always)
Best used whenCount knownCount unknownMust run at least once
Update locationIn for() headerInside bodyInside body

Infinite Loop

A loop that never ends. Use break to exit from inside.

C Language
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 run
  • do-while — body runs first, condition checked after; runs at least once
  • do-while needs semicolon: } while (cond);
  • Use while for unknown count, for for known count
  • Always update the loop variable to avoid infinite loops

while Loop

while loop तब तक code block repeat करता है जब तक उसकी condition true है। इसे use करते हैं जब पहले से नहीं पता कि loop कितनी बार चलेगा।

Syntax
while (condition) {
    // body
    // variable update (infinite loop से बचने के लिए!)
}

Condition पहले check होती है। अगर शुरू से false है तो body कभी execute नहीं होती।

C Language – 1 से 5
int i = 1;
while (i <= 5) {
    printf("%d\\n", i);
    i++;
}
1 2 3 4 5

Number Reverse करना

C Language
int num = 12345, rev = 0;
while (num != 0) {
    rev = rev * 10 + num % 10;
    num /= 10;
}
printf("Reverse: %d\\n", rev);
Reverse: 54321

do-while Loop

do-while while जैसा है, लेकिन body कम से कम एक बार execute होती है क्योंकि condition body के बाद check होती है।

Syntax
do {
    // body (कम से कम एक बार चलेगा)
} while (condition);  // यहाँ semicolon ज़रूरी है!
C Language – Menu Program
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 में अंतर

Featureforwhiledo-while
Condition checkपहलेपहलेबाद में
Minimum executions0 बार0 बार1 बार हमेशा
कब use करेंCount पता होCount नहीं पताएक बार ज़रूर चलना हो

सारांश

  • while — condition पहले check होती है; body शायद कभी न चले
  • do-while — body पहले चलती है, condition बाद में; कम से कम एक बार चलती है
  • do-while में semicolon ज़रूरी: } while (cond);
  • Infinite loop से बचने के लिए loop variable update करना न भूलें
← Back to C Tutorial