What is loop in C-programming language?
Looping statements execute the sequence of statements many times until the stated condition becomes false.
A loop in C consists of two parts, a body of a loop and a control statement. The control statement is a combination of some conditions that direct the body of the loop to execute until the specified condition becomes false.
The purpose of the C loop is to repeat the same code a number of times.
There are three types of loops present in C-programming language :
1. While Loop :
A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
Example:
#include<stdio.h>
int main()
{
int a = 10;
while(a < 20)
{
printf("Value of a: %d\n",a);
a++;
}
return 0;
}
It will print the value of a : from 10 to 19.
2. Do While Loop :
![]() |
do while loop in c |
Unlike while loop, which check the loop condition at the top of the loop, the do.. while loop in C programming checks its condition at the bottom of the loop.
A do... while loop is Similar to a while loop, except the fact that it is guaranteed to execute at least one time.
Example:
#include<stdio.h>
int main()
{
int a = 10:
do
{
printf("value of a : %d\n", a);
a = a + 1;
}while(a < 20);
return 0;
}
The above code is executed as :
value of a : 10
.
.
.
value of a : 19
3. For Loop :
![]() |
for loop in c |
A for loop is a repetition control structure that allows us to efficiently write a loop that needs to execute a specific number of times.
Example:
#include<stdio.h>
int main()
{
int a;
for(a = 10;a < 20;a = a + 1)
{
printf("value of a : %d\n",a);
}
return 0;
}
Output:
value of a: 10
value of a: 11
.
.
value of a : 19
1 Comments
Thanks for sharing!
ReplyDeletePost a Comment