Skip to main content

Syntax of while loop

Short answer

while (condition) { statements; } is the syntax. The condition is evaluated first; the loop body executes only while it is true. It is an entry-controlled loop requiring prior initialization and in-loop updating of the condition variable.

Write the syntax used in C programming language for while.

Syntax:

while (condition)
{
    statements;
}

The condition is evaluated first; the body is executed as long as it is true (non-zero). It is an entry-controlled loop, and the variable in the condition must be initialised before it and updated inside the loop.

Example:

#include <stdio.h>
int main(void)
{
    int i = 1;
    while (i <= 3)
    {
        printf("%d\n", i);
        i++;
    }
    return 0;
}

Output:

1
2
3

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 5 questions →