• 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

 

Categorized in: