While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition.
It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance.
The syntax of while loop in c language is given below:
while(condition){
//code to be executed
}
// simple program of while loop that prints table of 1.
#include<stdio.h>
int main(){
int i=1;
while(i<=10){
printf("%d \n",i);
i++;
}
return 0;
}
Program to print table for the given number using while loop in C
#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10){
printf("%d \n",(number*i));
i++;
}
return 0;
}
#include<stdio.h>
int main ()
{
int j = 1;
while(j+=2,j<=10)
{
printf("%d ",j);
}
printf("%d",j);
}
#include<stdio.h>
int main ()
{
while()
{
printf("hello World");
}
}
#include<stdio.h>
int main ()
{
int x = 10, y = 2;
while(x+y-1)
{
printf("%d %d",x--,y--);
}
}
If the expression passed in while loop results in any non-zero value then the loop will run the infinite number of times.
while(1){
//statement
}