For Loop in c Programming with Examples

C programming for loop; In this tutorial, i am going to show you what is for loop in c programming and how to use for loop in C programming with examples.

C Programming For Loop

Here, i will teach you about for loop in c programming language with examples; is as follows:

  • Definition of For Loop
  • Syntax of for loop in c
  • Example 1 – C program to print 1 to 10 numbers using for loop
  • Example 2 – C program to print even numbers from 1 to 10 using for loop
  • Example 3 – C program to print odd numbers from 1 to 10 using for loop

Definition of For Loop

In C programming, a for loop is used to repeat a block of statements until a specified condition is satisfied. And It is also known as an entry-controlled loop.

Syntax of for loop in c

The syntax of for loop in c; as shown below:

for (initializationStatement; testExpression; updateStatement)
{
    // statements inside the body of loop
}

Illustration of above given c for loop syntax

  • In For loop itetartion; Initialization statement is executed only once.
  • Then test expression is evaluated to true, statements inside the body of the for loop are executed, and the update expression is updated.
  • OR test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated.
  • Again the test expression is evaluated.
  • This process goes on until the test expression is false. When the test expression is false, the loop terminates.

Example 1 – C program to print 1 to 10 numbers using for loop

Let’s create c program to print 1 to 10 number using for loop; as shown below:

// Print numbers from 1 to 10
#include <stdio.h>
int main() {
  int i;
  for (i = 1; i < 11; ++i)
  {
    printf("%d ", i);
  }
  return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

Illustration of above given c program to print 1 to 10 number using for loop

  • Initialized i variable with value 1.
  • Intialized body of for loop with test condition.
  • Inside for loop; print i value. And The body of for loop is executed, until test condition is met true.
  • Update statement ++i is executed and the test expression i < 11 is evaluated.
  • This process goes on until i becomes 11.
  • When test condition will be false, and the for loop terminates.

Example 2 – C program to print even numbers from 1 to 10 using for loop

Let’s create c program to print even numbers from 1 to 10 using for loop; as shown below:

#include<stdio.h>
 
int main()
{
  	int i;
  	int  number = 10;
 
  	printf("\n The even number from 1 to 10 : ");
  
  	for(i = 1; i <= number; i++)
  	{
    	if ( i % 2 == 0 ) 
    	{
  			printf(" %d\t", i);
    	}
  	}
 
  	return 0;
}

Output

The even number from 1 to 10 :  2	 4	 6	 8	 10

Illustration of above given C program to print even numbers from 1 to 10 using for loop

  • Initialized i variable with value 1.
  • Initialized number variable with value 10.
  • Iterate body of for loop until the condition met true else terminate for loop exection.
  • Inside for loop body; use i%2 ==0 test condition to find even number from 1 to n and print it.

Example 3 – C program to print odd numbers from 1 to 10 using for loop

Let’s create c program to print odd numbers from 1 to 10 using for loop; as shown below:

#include<stdio.h>
 
int main()
{
  	int i;
  	int  number = 10;
 
  	printf("\n The odd number from 1 to 10 : ");
  
  	for(i = 1; i <= number; i++)
  	{
    	if ( i % 2 != 0 ) 
    	{
  			printf(" %d\t", i);
    	}
  	}
 
  	return 0;
}

Output

The odd number from 1 to 10 :  1	 3	 5	 7	 9

Illustration of above given C program to print odd numbers from 1 to 10 using for loop

  • Initialized i variable with value 1.
  • Initialized n variable with value 10.
  • Iterate body of for loop until the condition met true else terminate for loop exection.
  • Inside for loop body; use i%2 !=0 test condition to find odd number from 1 to n and print it.

Be First to Comment

Leave a Reply

Your email address will not be published.