while Loop in C
Notes:
while Loop in C Programming Language:
- Computer executes body of the while loop, as long as the given conditional expression evaluates to true. If the given conditional expression evaluates to false then it terminates the loop.
Syntax:
initialization;
while(conditional expression) // head of the while loop
{
statement(s); // body of the while loop
increment/decrement;
}
Ex: Program to display Hello World 5 times using while loop
int counter=0;
while(counter<5) // head of the while loop
{
printf("Hello World\n"); // body of the while loop
counter++;
}
Note:
When we don’t know exactly how many # of times the body of the loop is executed, we use while loop.
When we don’t know exactly how many # of times set of statements need to be executed, we use while loop.