How to use while loop in C Programming

 

Sometimes in C program we need to repeat statements for certain number of times. So in order to repeat statements for number of times we use repetition structure in C programming. There are 3 types of repetition structures available in C programming namely while, do-while and for. Following simple program explains how to use while repetition structure (also called as while loop) to print numbers from 1-10.
#include <stdio.h>

int main(void){
   // while loop is a type of repetition structure in C programming. In any loop you need to
   //fulfill following 4 conditions
   int i; // 1st condition: define control variable
   i = 1; // 2nd condition: assign initial value to control variable
   while(i<=10){ // 3rd condition: define loop termination condition
      printf("%d\n", i);
      i++; // 4th condition: increment/decrement control variable
   }

   return 0;
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *