Learn one of the most powerful programming languages in the world and become a rockstar developer.

WHILE LOOP Statement

In this tutorial, you will learn to create while and do...while loop in C programming with the help of examples.

The C while loop is used when you want to execute a block of code repeatedly with a checked condition before making an iteration.

C programming has three types of loops.

  1. for loop
  2. while loop
  3. do...while loop

While loop

The following illustrates the syntax of the for loop statement:

while (testExpression) 
    // statements inside the body of the loop 

The while loop executes as long as the given logical expression evaluates to true. When expression evaluates to false, the loop stops. The expression is checked at the beginning of each iteration. The execute statements inside the body of the while loop statement are not executed if the expression evaluates to false when entering the loop. It is necessary to update the loop condition inside the loop body to avoid an indefinite loop.

// Print numbers from 1 to 5
#include <stdio.h>
int main()
    int i = 1;
    while (i <= 5)
        printf("%d\n", i);
    return 0;
Output :

Explanation :
  1. At first, i is initialized to 1.
  2. The test expression i <= 5 is evaluated. Since 1 less than 5 is true, the body of while loop is executed. This will print the 1 (value of i) on the screen.
  3. The update statement ++i is executed. Now, the value of i will be 2. Again, the test expression is evaluated to true, and the body of while loop is executed. This will print 2(value of i) on the screen.
  4. Again, the update statement ++i is executed and the test expression i <= 5 is evaluated. This process goes on until i becomes 6.
  5. When i becomes 6, i <= 6 will be false, and the while loop terminates.

Do While Loop

To run a block of code repeatedly in a predetermined time, you use the for loop statement. In cases you want to run a block of code repeatedly based on a given condition with a check at the end of each iteration, you use the do while loop statement.

The following illustrates the syntax of the do while loop statement:

   // statements inside the body of the loop
while (testExpression);
#include <stdio.h>
int main () {

   /* local variable definition */
   int a = 10;

   /* do loop execution */
   do {
      printf("value of a: %d\n", a);
      a = a + 1;
   }while( a < 20 );
   return 0;
Output :

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Explanation :

Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.

If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.

Previous Next
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine