while loop in C Programming Language

while loop in C Programming Language


In general, while loop is like as other loops. But this loop is mostly used where the number of iterations is unknown in advance. It is also known as a pre-tested loop.

Syntax:

    while(condition){  
//code to be executed
}

Flowchart of while loop in C Programming Language

while loop in C Programming Language

Example 1:

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

After executing the program our output looks like

1
2
3
4
5
6
7
8
9
10

Example 2:

 #include<stdio.h>  
int main(){
int i=1,number=0,b=9;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10){
printf("%d n",(number*i));
i++;
}
return 0;
}

After executing the program our output looks like

Enter a number: 50
50
100
150
200
250
300
350
400
450
500

Properties of while loop

ü  while loop will repeatedly execute until the given condition fails.

ü  If our given condition will be true, then it returns 0. And if our condition will false, then it returns any non-zero number.

ü  In while loop, condition is compulsory. We can run a while loop without a body

ü  We can use multiple condition in while loop.

Infinitive while loop in C Programming Language

The loop will run the infinite number of times, if we pass 1 in while loop.
Syntax:

    while(1){  
//statement
}
Post a Comment