For Loop in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

The for loop in C programming language is a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

Table Of Contents

For loop Syntax in C

 for (expression1; expression2; expression3) 
 {
   //statements
 }

Here expression1 is called initialization expression, expression2 is called test expression, and expression3 is called update expression.

The initialization expression is used to set the initial value of the loop control variable before the loop begins. This expression executes only once.

The test expression is the condition of for loop. If the condition is true then the statements of the for loop executes. Looping continues as long as the condition is true. When the condition becomes false then execution of for loop is terminated.

The update expression defines how the initialization expression will change each time to the loop is repeated. It also involves the loop control variable.

Example:-

 for(int i=1; i<=5; i++) 
 {
   printf("%d ",i);
 }

Flowchart of the for loop

Flowchart of the for loop

For loop in C programming example

Based on the above points now we will write some programs using for loop.

C Program to Print N Natural Numbers using for loop

Here we will write a C program using for loop to print N natural numbers.

 #include<stdio.h>
 int main()
 {
   int n;

   printf("Enter n value: ");
   scanf("%d",&n);

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

   return 0;
 }

Output:-

Enter n value: 10
1 2 3 4 5 6 7 8 9 10

Print Even Numbers in the Range

 #include<stdio.h>
 int main()
 {
   int n;

   printf("Enter n value: ");
   scanf("%d",&n);

   for(int i=1; i<=n; i++)
   {
     if(i%2==0) printf("%d ",i);
   }

   return 0;
 }

Output:-

Enter n value: 15
2 4 6 8 10 12 14

C Program to Find Sum and Average of N Natural Numbers using For loop

 #include<stdio.h>
 int main()
 {
   int n, sum;
   float avg;

   printf("Enter n value: ");
   scanf("%d",&n);

   sum = 0;

   for(int i=1; i<=n; i++)
   {
     sum = sum + i;
   }

   avg = ((float)sum)/n;
   printf("Sum = %d \t Average = %.2f",sum,avg);

   return 0;
 }

Output:-

Enter n value: 10
Sum = 55 Average = 5.50

Nested for loop in C

The for loop inside another for loop is called nested for loop. The syntax of nested for loop is given below,

 for (exp1; exp2; exp3) 
 {
   for (exp4; exp5; exp6)
   {
     // code
   }
 }

In nested loops, the inside loop executes completely before the outside loop next iteration. There can be any number of for loops inside for loop.

Nested For loop Programs in C with output

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

   return 0;
 }

Output:-

1 1
1 2
2 1
2 2
3 1
3 2

Note that in nested for loops, the initialization variables (in this case i and j) should be different. If you take the same variables then you will get an unexpected result.

You will see the use of nested for loop in pattern programs, multidimensional arrays such as matrix operations. Pattern programs and Matrix operates with rows and columns. The outer for loop works on rows and the inner for loop works on columns.

Additional Forms of for loop

Point1. The body of the loop in for statement need not contain any statement at all.

The below program have a semicolon at the end of for loop.

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

Output:-

4

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

Output:-

1
2
3

Point2. In for loop the two semicolons within the parenthesis are compulsory. The expressions of for loop may be blank. The initialization of for loop control variable can be done outside of for loop. The update statement can be placed within the statements of for loop instead of update expression.

 #include<stdio.h>
 int main()
 {
   int i=1;
   for(;i<=5;)
   {
     printf("%d\t",i++);
   }
   return 0;
 }

Output:-

1 2 3 4 5

Point3. If the initialization, condition, update expression of for loop is omitted, then it becomes an infinite loop, and it is similar to while true.

 #include<stdio.h>
 int main()
 {
   int i;
   for(;;)
   return 0;
 }

To stop the infinite loop, we should use the break statement.

 #include<stdio.h>
 int main()
 {
   int i=1;
   for(;;)
   {
     if(i>5) break;
     else printf("%d\t",i++);
   }
   return 0;
 }

Output:-

1 2 3 4 5

Point4. If the condition of for loop becomes false at the initial state, then the statements of for loop never executed. It is similar to while false.

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

We don’t get any output from this program.

Points on Expressions of for loop

Point5. The condition of for loop can a compound expression made up of various relational expressions connected by logical AND (&&), OR (||) operators. The loop testing needs not to be limited only to the loop control variable i.e it can be different from the loop control variable.

 #include<stdio.h>
 int main()
 {
   int j, sum=0;
   for(j=1; j<=25 && sum<=50; j++)
   {
     sum = sum + j;
   }
   printf("j=%d\n",j);
   printf("Sum=%d\n",sum);
   return 0;
 }

Output:-

j=11
Sum=55

Point6. The for loop may have more than one initialization variable, and update expression. They should be separated by using the comma operator.

 #include<stdio.h>
 int main()
 {
   for(int i=1, j=5; i<10 && j>0; i++, j--)
   {
     printf("%d %d\n",i,j);
   }
   return 0;
 }

Output:-

1 5
2 4
3 3
4 2
5 1

Here there are two initialization variables i and j, they are separated by using a comma operator. This for loop has two update expressions i++ and j--.

Point7. for loop may have more than one conditional expression separated with a comma operator. In this case, the rightmost part decides the condition.

 #include<stdio.h>
 int main()
 {
   int a, b;
   for(a=1, b=3; a<=2, b<=7; a++, b++)
   {
     printf("%d %d\n",a,b);
   }
   return 0;
 }

Output:-

1 3
2 4
3 5
4 6
5 7

Here, the left-hand operand of comma expression has no effect. So, a<=2 has no importance in this program.

Difference Between a while & for loop in C

Prerequisites:- while loop and do-while loop in C

The for contains the initialization, update, and test_condition in one statement, so it makes for very readable code. All the control steps, initialization, end-of-loop test, and updating are done in one place.

for vs while loop

for vs while loop code in c

Bounded and Unbounded loops

In a bounded loop, repetition is implemented by contracts that allow a determinate number of iterations i.e. It should be used when we know, ahead of time, how many times we need to loop.

The for loop is generally called a determinate or definite or bounded loop because the programmer knows exactly how many times it will repeat. The number of repetitions can be determined mathematically by manually checking the logic of the loop.

In an unbounded loop, one doesn’t know, ahead of time, how many iterations may be required.

The while and do-while loop are also called indeterminated or indefinite or unbounded loop. It used where the number of iteration is not known in advance, or when iteration depends on the input.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Recommended Posts

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

2 thoughts on “For Loop in C”

Leave a Reply