For Loop in c Programming Examples

For Loop in c Programming Examples

C programming for loop; Through this tutorial, you will learn how to use for loop in C programming with examples.

C Programming For Loop

  • 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
}

Explanation 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

See the following 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

Explanation of above 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

See the following 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

Explanation above 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

See the following 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

Explanation above 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.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *