How to use while loop in C Language

While loop

On this page

While loop

Program to use while loop in C Language

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

Output

  1 2 3 4 5
  

Steps Description

  • Declare integer variable
  • Initialize in integer variable i = 1
  • While(i<=5) condition which needs to be evaluated
  • pintf inbuild function to display the value of %d
  • code of instructions which needs to be executed
  • i++ increment value