While loop in c


  • A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
  • Its called a loop because control keeps looping back to the start of the statement until the test becomes false.
  • The test condition is indicated at the top and it tests the value of the expression before processing the body of the loop.
  • The test condition may be any expression. The loop statements will be executed till the condition is true, i.e. the test condition is evaluated and if the condition is true, then the body of the loop is executed.
  • When the condition becomes false, the execution will be out of the loop.

    Syntax:

    while(condition)
      {
         statements(s);
      }
    

Flow Diagram while loop:

    while loop in c

    Example:

    #include <stdio.h>
    int main()
    {
       int count=1;
       while (count <= 5)
       {
    	printf("%d ", count);
    	count++;
       }
       return 0;
    }
    

    Output:

    1 2 3 4 5