When we need to execute a piece of code many times then instead of writing the code repeatedly we use loop. Loop will repeat the code until it meets its condition.
We have three following type of loop:-
In this tutorial, we will learn about for loop.
for(initialization ; Condition ; Step-value)
{
Programming Statement
}
- Initialization: - from where loop will start.
- Condition: - when loop will be finished
- Step-value: - increment and decrement value
In the following example, we will print hello world statement 5 times using for loop.
#include<stdio.h>
void main()
{
int i;
for(i=1; i<=5;i++)
{
printf("Hello World");
printf("\n");
}
}
In the above example first for loop initialize i variable with 1. In second process, condition will be checked. If condition will be true then loop will be executed and step value will be performed. Again, condition will be checked and process will be repeated continuously until condition will be false.
Figure 1
#include<stdio.h>
void main()
{
int i;
for(i=1; i<=10;i++)
{
printf("%d",i);
printf("\n");
}
}
Figure 2