WHILE LOOP IN C

Maha


While Loop In C

A while loop is a control flow statement that repeatedly executes a block of code as long as the specified condition is true. 

Syntax:

while (condition) 
{
// code to be executed repeatedly 
}

Example Of While Loop In C To Calculate The Sum Of Numbers From 1 To 20:

Program:


  #include <stdio.h>
  int main(){
  int i = 1;
  int sum = 0;
  while (i <= 10){
      sum += i;
      i++;
  }
  printf("The sum of numbers from 1 to 20 is %d\n",sum);
  return 0;
 }


Output:


  The sum of numbers from 1 to 20 is 210




More topic in C

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send