YourCodingMentor

The do...while loop in C is similar to the while loop, but with one key difference: the condition is checked after the code inside the loop is executed, ensuring that the loop runs at least once, regardless of whether the condition is true or false initially.


Syntax

do {
    // Code to be executed
} while (condition);
  • condition: An expression that is evaluated after the code block has been executed. If the condition evaluates to true (non-zero), the loop continues; otherwise, it terminates.

Key Difference Between while and do...while:

  • In a while loop, the condition is evaluated before each iteration, so the code may not run at all if the condition is initially false.
  • In a do...while loop, the code is executed at least once because the condition is checked after the code block is executed.

Example 1: Basic do...while Loop

#include <stdio.h>

int main() {
    int i = 1;

    do {
        printf("Iteration %d\n", i);
        i++; // Increment i
    } while (i <= 5);

    return 0;
}

Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

In this example, the do...while loop runs as long as i is less than or equal to 5. The code inside the loop is executed at least once, even if the condition is false initially (though in this case, the condition is true for all iterations).

Example 2: do...while with Condition False Initially

Here, the condition is false from the start, but the loop executes at least once.

#include <stdio.h>

int main() {
    int i = 6;

    do {
        printf("Iteration %d\n", i);
        i++; // Increment i
    } while (i <= 5);

    return 0;
}

Output:

Iteration 6

Even though i starts out as 6, the code inside the loop is executed once before checking the condition. After that, the condition is false, so the loop terminates.

Example 3: Using break to Exit a do...while Loop

The break statement can be used to exit the do...while loop prematurely.

#include <stdio.h>

int main() {
    int i = 1;

    do {
        if (i == 3) {
            break;  // Exit the loop when i equals 3
        }
        printf("Iteration %d\n", i);
        i++;
    } while (i <= 5);

    return 0;
}

Output:

Iteration 1
Iteration 2

In this case, when i reaches 3, the break statement is executed, and the loop terminates immediately.

Example 4: Using continue in a do...while Loop

The continue statement skips the remaining code in the current iteration and proceeds to the next iteration.

#include <stdio.h>

int main() {
    int i = 1;

    do {
        if (i == 3) {
            i++;  // Increment i before continuing to avoid infinite loop
            continue;  // Skip the rest of the code for i == 3
        }
        printf("Iteration %d\n", i);
        i++;
    } while (i <= 5);

    return 0;
}

Output:

Iteration 1
Iteration 2
Iteration 4
Iteration 5

In this example, when i equals 3, the continue statement is executed, which skips the printf for that iteration and moves to the next one.

Example 5: Using do...while with User Input

You can use the do...while loop to repeat an action based on user input, ensuring the loop executes at least once.

#include <stdio.h>

int main() {
    int number;

    do {
        printf("Enter a positive number: ");
        scanf("%d", &number);

        if (number <= 0) {
            printf("Non-positive number entered, exiting...\n");
            break;  // Exit the loop if a non-positive number is entered
        }

        printf("You entered: %d\n", number);
    } while (number > 0);

    return 0;
}

Output:

Enter a positive number: 10
You entered: 10
Enter a positive number: -5
Non-positive number entered, exiting...

In this example, the loop prompts the user to enter a positive number. If a non-positive number is entered, the loop terminates using the break statement.


Summary

  • The do...while loop guarantees that the code block inside the loop is executed at least once, even if the condition is initially false.
  • The condition is checked after the execution of the loop body.
  • break can be used to exit the loop prematurely, and continue can be used to skip the current iteration and proceed to the next one.
  • The do...while loop is useful when you need the loop to execute at least once before checking the condition (e.g., menu-driven programs or prompts for user input).

Leave a Reply

Your email address will not be published. Required fields are marked *