Skip to main content

While loop with syntax and flowchart

Short answer

The while loop is an entry-controlled loop that tests a condition before executing the body. Its syntax: initialisation; while (condition) { statements; update; }. The flowchart shows a condition diamond; if true, execute body and update then loop back; if false, exit.

Explain about while loop with its syntax and flowchart.

The while loop is an entry-controlled loop: the condition is tested first, and the body is executed repeatedly as long as the condition is true. If the condition is false at the start, the body never executes.

Syntax:

initialisation;
while (condition)
{
    statements;
    update;
}

Flowchart:

      [ initialisation ]
              |
        +---->|
        |     v
        | < condition ? >--False--> next statement
        |     | True
        | [ body + update ]
        +-----+

Example:

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

Output:

Sum of 1 to 5 = 15

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 5 questions →