Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

while — pre-test loop

A while loop checks its condition before each iteration. It repeats while the condition is nonzero and may never execute its body.

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

Syntax

while (expression) {
    /* loop body */
}

while checks its controlling expression before each body execution during normal loop entry. A value equal to zero ends the loop, so the body may never execute.

The expression must have a scalar type: an arithmetic type, a pointer type, or nullptr_t. The body can be a single statement or a { ... } block. Beware: a semicolon immediately after while (expression) creates an empty body.

In the example, count takes values from 1 through 3. The ++count operation ensures progress; at 4, count <= 3 is false.

Examples

#include <stdio.h>

int main(void)
{
    int count = 1;
    while (count <= 3) {
        printf("%d\n", count);
        ++count;
    }
    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