while loop

The syntax of a while loop is:

while (testExpression) 

where, testExpression checks the condition is true or false before each loop.

How while loop works?

The while loop evaluates the test expression.

If the test expression is true (nonzero), codes inside the body of while loop are exectued. The test expression is evaluated again. The process goes on until the test expression is false.

When the test expression is false, the while loop is terminated.

Flowchart of while loop

flowchart of while loop in C programming

Example #1: while loop

// Program to find factorial of a number

// For a positive integer n, factorial = 1*2*3...n

#include <stdio.h>
int main()
    int number;
    long long factorial;

    printf("Enter an integer: ");

    factorial = 1;

    // loop terminates when number is less than or equal to 0
    while (number > 0)
        factorial *= number;  // factorial = factorial*number;

    printf("Factorial= %lld", factorial);

    return 0;


Enter an integer: 5

Factorial = 120

To learn more on test expression (when test expression is evaluated to nonzero (true) and 0 (false)), check out relational and logical operators.