What is While Loop in C Language with example ?

  • While loop evaluates the conditions inside the brackets.
  • If the condition returns true, the statements inside the while loop body will be executed.
  • Condition inside the while loop is executed once again.
  • This process goes on until the condition in the while loop becomes false.
  • Once the condition is false, while loop terminates.

Syntax of While loop

while (testExpression) {
  // the body of the loop 
}

Sample Code

// Print numbers from 1 to 5

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

  return 0;
}

Output

1
2
3
4
5

 

Leave a Reply

Your email address will not be published.

You May Also Like