Site icon TestingDocs.com

C for loop statement

Overview

C for loop statement allows us to repeat a statement or group of statements more than once. The programmer can use this loop when the number of iterations is known beforehand. For example, to iterate over the array elements of fixed size.

Syntax

The general syntax for the C for loop is as follows:

for (loop initialization; condition; increment/decrement)
{
// for loop statements
}

Example

Sample program to illustrate the for loop in C language.

 

/**
**********************************
* Program Description:
* C for Loop Statement Demo
* Filename : forDemo.c
* C Tutorials – www.TestingDocs.com
*************************************
*/
#include<stdio.h>
int main()
{
int i;// for loop counter variable

for(i= 1; i<= 10; i++)// C for loop statement
{
printf(” Loop Iteration (%d) \n”,i);
} // end for loop

return 0;
} // end main

In this example, we have declared a for loop counter variable i.  The loop executes 10 times. The loop executes for values i <= 10. When the variable i reaches 11 the loop condition i <= 10 is false. The loop will exit.

Output

C Tutorials

C Tutorials on this website can be found at:

https://www.testingdocs.com/c-language-tutorial/

Exit mobile version