Loop in C Language

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.

Type of Loop

We have three following type of loop:-



In this tutorial, we will learn about for loop.

Syntax of 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


Example of for loop

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.

		
		loop in c language
		Figure 1
		
		


Program to print counting from one to 10

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

		
		
		
		for loop in c
		Figure 2