while loop in c

while loop in c

The syntax of while statement or while loop is-

while (expression)
{
statement
statement
statement
………
………
}

Here it is also like selection statements if-else we can have single or multiple statement inside the while loop and lets us know how the while loop works by flowchart

whileloopinc

first the expression is evaluated. if it is true then all statements in the body of the while loop are executed. after the execution, cursor return to the expression so again expression is evaluated and if it is found to be true again then again the statements in the body of the loop are executed. That is mean these all sentences are executed of the body till the expression is true and when it becomes false, the loop terminates and the control comes out of the loop.

#include<stdio.h>
int main()
{
int i;
while(i<=100)
{
printf("%d",i);
}
return 0;
}

#include<stdio.h>
int main ()
{
/* local variable definition */
int a = 5;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
return 0;
}