what is do while loop in c

10 months ago 25
Nature

The do...while loop in C is a control flow statement that creates a structured loop that executes as long as a specified condition is true at the end of each pass through the loop. It is similar to the while loop, but with one important difference: the body of the do...while loop is executed at least once, regardless of the condition, and then the test expression is evaluated. The syntax of the do...while loop in C programming language is as follows:

do {
    // body of the loop
} while (condition);

Heres an example of how to use the do...while loop in C:

#include <stdio.h>

int main() {
    // loop variable declaration and initialization
    int i = 0;

    // do while loop
    do {
        printf("Geeks\n");
        i++;
    } while (i < 3);

    return 0;
}

Output:

Geeks
Geeks
Geeks

In summary, the do...while loop in C is a loop statement used to repeat some part of the code until the given condition is fulfilled, and it is guaranteed to execute at least once.