Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

do — post-test loop

The do keyword starts a do-while loop: the body executes first, and the condition is checked afterward.

Version: C23; repository compiler flag, production runtime not attested

Syntax

do {
    /* statements */
} while (condition);

do starts a post-test loop. On normal entry, its body executes at least once, even if the condition is initially false. An ordinary while checks its condition before the body.

The condition must have scalar type. The loop repeats while the condition compares unequal to zero; zero ends repetition. The semicolon after while (condition) is required. Braces group the body statements.

In the example, count increases before count < 3 is checked, so the program prints 1, 2, and 3 on separate lines.

Examples

#include <stdio.h>

int main(void)
{
    int count = 0;

    do {
        ++count;
        printf("%d\n", count);
    } while (count < 3);

    return 0;
}

Result

1
2
3

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources