What is Loop in C?
Looping Statements in C 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.
For loop in C
A for loop is a more efficient loop structure in ‘C’ programming. The general
structure of for loop syntax in C is as follows:
Syntax of For Loop in C:
for (initial value; condition; incrementation or decrementation )
{
statements;
}
#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
While Loop in C
A while loop is the most straightforward looping structure. While loop syntax in C
programming language is as follows:
Syntax of While Loop in C:
while (condition) {
statements;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Do-While Loop
Syntax of do while loop in C programming language is as follows:
Syntax of Do-While Loop in C:
do {
statements
} while (expression);
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10