While Loop in C programming
This article is about the while loop in C and the usage of it in C programs.
While loop in C
While loop tests the condition before executing the loop body. It repeats a statement or group of statements until a given condition is true.
In general, a while loop allows a part of the code to be executed multiple times depending upon a given condition. It can also be called as an iterating if statement. While loops are used when the number of iterations is unknown but the loop termination condition is known. If Condition is true, the flow re-iterates and when Condition is false, the flow exits the loop.
Condition Expression is mandatory in while loop and it is possible to run a while loop without the body. While loop allows us to have more than one conditional expression. Braces in the loop can be skipped if the body has only one statement.
Syntax
while (condition)
{
//statements inside the body of the loop
}
Example
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d ", i);
++i;
}
return 0;
}
Output
1 2 3 4 5