C Programs Tutorials | IT Developer
IT Developer

C Programming - C do while Loop



Share with a Friend

C Programming - C do while Loop

do-while Loop in C

The do-while loop is a control flow statement that is similar to the while loop but with a key difference: the condition is checked after each iteration, not before. This guarantees that the code inside the loop will be executed at least once, even if the condition is false initially.

Syntax of do-while Loop:

C

do {

    // Code to be executed

} while (condition);

  • do: This keyword marks the start of the loop.
  • while (condition): The condition is checked after each iteration. If it is true, the loop continues; if false, the loop terminates.

How do-while Loop Works:

  1. Loop Body Execution: The body of the loop is executed once before checking the condition.
  2. Condition Check: After the loop body executes, the condition is evaluated.
  3. Iteration: If the condition is true, the loop executes again. If it is false, the loop exits.

Example:

C

#include <stdio.h>

int main() {

    int i = 0;

    // A simple do-while loop that prints numbers from 0 to 4

    do {

        printf("i = %d\n", i);

        i++;  // Increment i

    } while (i < 5);

    return 0;

}

Explanation:

  • The loop will execute the body at least once, printing the value of i.
  • After each iteration, i is incremented.
  • The condition i < 5 is checked after the body of the loop.

Output:

i = 0

i = 1

i = 2

i = 3

i = 4

Key Characteristics of do-while Loop:

  1. Post-Test Loop: The condition is checked after the loop body is executed. This ensures that the loop body runs at least once.
  2. Always Executes Once: The loop body executes at least once, even if the condition is false on the first check.
  3. Condition Checked After Execution: If the condition is false initially, the body still executes once before the loop exits.

Example with User Input:

Here’s an example of a do-while loop with user input. The loop continues asking the user to enter a number until they enter 0.

C

#include <stdio.h>

int main() {

    int number;

    // Ask the user for input at least once

    do {

        printf("Enter a number (0 to quit): ");

        scanf("%d", &number);

        printf("You entered: %d\n", number);

    } while (number != 0);

    printf("Exited the loop.\n");

    return 0;

}

Explanation:

  • The loop asks the user to enter a number.
  • The condition is checked after printing the number. If the user enters 0, the loop exits.

Sample Output:

Enter a number (0 to quit): 5

You entered: 5

Enter a number (0 to quit): 10

You entered: 10

Enter a number (0 to quit): 0

You entered: 0

Exited the loop.

Nested do-while Loop:

You can also nest a do-while loop inside another do-while loop. The inner loop will run completely for each iteration of the outer loop.

Example:

C

#include <stdio.h>

int main() {

    for (int i = 1; i <= 3; i++) {

        printf("Outer loop i = %d\n", i);

       

        // Inner do-while loop

        int j = 1;

        do {

            printf("Inner loop j = %d\n", j);

            j++;

        } while (j <= 3);

    }

    return 0;

}

Explanation:

  • The outer for loop runs 3 times.
  • The inner do-while loop runs 3 times for each iteration of the outer loop.

Output:

Outer loop i = 1

Inner loop j = 1

Inner loop j = 2

Inner loop j = 3

Outer loop i = 2

Inner loop j = 1

Inner loop j = 2

Inner loop j = 3

Outer loop i = 3

Inner loop j = 1

Inner loop j = 2

Inner loop j = 3

do-while Loop with break and continue :

  • break: Terminates the loop immediately.
  • continue: Skips the current iteration and moves to the next iteration.

Example with break and continue:

C

#include <stdio.h>

int main() {

    int i = 0;

    do {

        if (i == 5) {

            break;  // Exit the loop when i equals 5

        }

        if (i % 2 == 0) {

            i++;  // Increment and skip even numbers

            continue;  // Skip even numbers

        }

        printf("i = %d\n", i);  // Prints only odd numbers

        i++;

    } while (i < 10);

    return 0;

}

Explanation:

  • When i == 5, the break statement terminates the loop.
  • When i is even, the continue statement skips that iteration.

Output:

i = 1

i = 3

i = 5

Summary of do-while Loop:

  • The do-while loop guarantees that the loop body executes at least once, which makes it useful in scenarios where you need to perform an action before checking a condition (e.g., user input validation).
  • It is a post-test loop, meaning the condition is checked after the loop body is executed.

Advantages:

  • Ensures at least one execution of the loop body.
  • Ideal for scenarios where the action needs to be performed first (e.g., user prompts, initialization).

Disadvantages:

  • Can lead to unexpected behavior if not carefully managed, as the loop always executes at least once, even when the condition is false.