A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Table of Contents
Syntax
The syntax of a for loop in C programming language is :
for ( initialization ; condition; updateStatement) { statement(s); }
How for loop works?
The initialization statement is executed only once.
Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for
loop is executed and the update expression is updated.
This process repeats until the test expression is false.
The for loop is commonly used when the number of iterations is known.
To learn more on test expression (when test expression is evaluated to nonzero (true) and 0 (false)), check out relational and logical operators.
for loop Flowchart
Example
//Program to show for loop in c #include<conio.h> #include<stdio.h> // start main function void main() { int i = 0; clrscr(); //to clear the screen // starting for loop for (i=0 ; i <=10; i++){ printf("%d \n",i);//prints value of i } getch(); }
Output
1 2 3 4 5 6 7 8 9 10
Video showing sample :