View Course Path

For loop in C – Full explanation with examples and tutorials

A for loop is one of the most important and frequently used loops in C programming. As we will see in the examples below its primary use is to execute something in a loop for a particular count. For example, consider that you are working on an Arduino and you want to blink an LED five times. The blinking of the LED, is the event that you will put in a loop that executes five times. You can obtain this numbered execution using a for loop.

What is the syntax of a for loop in C?

A for loop is executed with the help of a variable entity known as the iterator. There are three main processes that an iterator goes through during the execution of a for loop.

  1. The iterator is first initialized
  2. Then the iterator is then tested for a condition
  3. The iterator is updated
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}

We will take a look at the properties for the three iterator statements in the section below.

What is a for loop? How does a for loop work?

A for loop is a tool that can be used to iterate a particular set of statements for a particular number of loops. It does this by checking for a preset condition. IT keeps executing until the condition is true.

Consider this example of a for loop. Notice the similarities between this example and the syntax above.

for (int i = 0; i < 10; i++) 
{
printf("%d\n", i);
}

Output and Explanation

0 1 2 3 4 5 6 7 8 9

The for loop begins by initializing a variable, ‘i’, also known as the iterator. This expression is known as the initialization statement. Its initial value is set at 0.

In the next statement, known as the test expression, the programmer sets a condition that controls the number of times the loop will execute. Here, this condition is set as ‘i<10’. This means that the loop will execute for as long as the value of ‘i’ remains less than 10.

Moving on, if the condition is true, which it is now, the control will enter the loop and the printf command will be executed, 0 will be printed and the control will exit the loop.

AFTER the execution of the first loop, the THIRD iterator statements will execute. Here, the value of the iterator ‘i’ is incremented by 1. This statement is known as the update statement. Subsequently, the control will return to the SECOND statement where the condition undergoes the test one more time. If held true, the loop executes one more time. And the cycle continues, until ‘i’ increases to 10, the condition becomes false and the control passes on to the statements after the loop.

Properties of the Initialization Statement in a for loop

  • We can initialize more than a single expression here
  • This is an optional statement

Properties of the test expression/conditional statement in a for loop

  • It’s a conditional expression
  • We can put more than one condition in this statement separated by a comma. In this case, the loop executes until all the conditions are met.
  • This statement can do the work of the other two statements as well.

Properties of the update statement in a for loop

  • We can update multiple variables in this statement
  • It is an optional statement

What does the flow chart of a for loop in C look like?

c-for loop flow chart working with examples

Example 1: Write a program to print or find all the even numbers between 1 and 30. Use a for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,x;
clrscr();
printf("All the even numbers between 1 and 30 are\n");
for(i=1;i<=30;i++)
{
x=i%2;
if (x==0)
{
printf("%d\n",i);
}
}
getch();
}

Output and Explanation

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30

The loop will execute 30 times. Starting from the value of ‘i’ equal to 1 to the value of ‘i’ equal to 30. At every iteration, the value of i is modulo divided by 2. And if the remainder is 0, the program outputs the current value of i.

Example 2: Write a program to print or find the sum of the first ten natural numbers

//print the sum of the first ten natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();//remove this if the statement is giving an error.
for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf("%d",sum);
getch();
}

Output and Explanation

55

Here we can see that the iterator was partly initialized outside the for-statement. However, it’s given a value in the for-statement. This is another possible way that you should remember. The loop, as you can see, will execute 10 times. Each time it will add the sum of the values of ‘i’ with the subsequent value of ‘i’. Once the loop is done executing 10 times, it will print the result.

Example 3: Write a program to print or generate a multiplication table of any number entered by the user

//generate the multiplication table of a number entered by user
#include<stdio.h>
#include<conio.h>
void main()
{
int i,table;
clrscr();
printf("Enter the number whose multiplication table you want\n");
scanf("%d",&i);
for(int j=1;j<=10;j++)
{
table=i*j;
printf("%d x %d = %d\n",i,j,table);
}
getch();
}

Output and Explanation

Enter the number whose multiplication table you want
5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

This is a cleverly simple program where a value is taken from the user to generate a multiplication table. The iterant is also the multiplier. As we know that in a multiplication table the multiplier increases its count by one. We also know that in a for loop we can increase the iterator by 1. By combining these two principles, we are generating a multiplication table. Ingenious. Isn’t it?

Example 4: Write a program to take a numeral input from the user and check if the number entered is prime or not. Use a for loop.

//take number for user and check if the number is prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a,b,temp1,temp2;
printf("Enter the number which you would like to check whether its prime or not\n");
scanf("%d",&i);
for(a=2;a<i;a++)
{
b=i%a;
if(b==0)
{
temp1=0;
}
else
{
temp2=1;
}
}
if(temp1==0)
{
printf("Number is not prime");
}
else
{
printf("Number is prime");
}
getch();
}

Output and Explanation

This one’s for you. Find the output for the program and try to explain it to yourself. Try it once and if you are unable to find the output, use one of the online IDEs here to help yourself.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.