The for loop in c is an entry -controlled loop which provides concise loop control structure. It gives you the power to control how much time a code you want to execute.

How many times for loop will be executed which decides on the three parameters of the “for” loop. initialization value, test expression, and increment/decrement statement.

Syntax of for Loop in C:-


1
2
3
4
5
6
7
for(initialization; test Expression; increment/decrement)

{

Body of Loop

}

If we explain the above for loop expression.

initialization: this allow you to initialize the initial value for the loop control.

test expression: This is for testing the condition. If the condition satisfy the enter into the for loop body and execute the code of for loop.

increment/decrement: This is for increment or decrement the value of variable each time after the execution of the for loop body.

Test expression is also know as condition evaluation. Each time first condition evaluated then body of for loop executed and at last the value of variable will be incremented.

We can increment the value by one, two or by any as per logic and also we can decrease the value of the variable after the execution of the for loop body.

Below are the example of for loop in c which will help you to understand the for loop in better way.

Write a program to print all the natural no. till 10 using for loop in c.

Solution:-

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1; i<=10; i++)
{
printf("%d\t",i);
}
getch();
}

Write a program to print all the natural no. from 10 in reverse order.

Solution:-

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=10; i>=1; i--)
{
printf("%d\t",i);
}
getch();
}

I hope you have understanding of for loop in c concept.