Loop in C language is used to make a part of the program or statements several times.
In the While Loop, the position is given before the statement. So it is different from the loop while doing the do. This statement can execute 0 or more times.
When use while loop in C
The C language while loop should be used if number of iteration is uncertain or unknown.
Syntax of while loop in C language
The syntax of while loop in c language is given below:
while(condition)
{
//code to be executed
}
Flowchart of while loop in C
Example of while loop in C language
Let's see the simple program of while loop that prints table of 1.
#include <stdio.h>
#include <conio.h>
void main(){
int i=1;
clrscr();
while(i<=10){
printf("%d \n",i);
i++;
}
getch();
}
Output
1
2
3
4
5
6
7
8
9
10
Program to print table for the given number using while loop in C
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,number=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10){
printf("%d \n",(number*i));
i++;
}
getch();
}
Output
Enter a number: 50
50
100
150
200
250
300
350
400
450
500
Enter a number: 100
100
200
300
400
500
600
700
800
900
1000
Infinitive while loop in C
If you pass 1 as a expression in while loop, it will run infinite number of times.
while(1){
//statement
}