In the Control Flow Statements, loops are one of the most useful and necessary part. Among the loops, for loop is the most familiar and used looping statement.
Syntax:
Syntax:
- for(initial; condition; Increment)
- {
- //Statements to be executed
- }
The above shown is the syntax of a for loop. A for loop contains three parts mainly.
- initial is the initial conditions we maintain before a loop starts. These are mostly assignment operators. We assign some variables, which are necessary before the loop here.
- condition is mostly a relation. This is the condition which controls, the loop. Based on the condition and its satisfaction, loop runs. If the condition is not satisfied, then the control gets out of loop.
- increment is the operation, where in generally used to increment some value to know the loop counter or to change some values at each iteration.
And if there is only one statement, we can write directly, otherwise, we write the statements in between the curly braces({...}).
Also we can nest for loops i.e., a loop inside another loop. Consider the following program to understand the for loop.
Example:
- #include<stdio.h>
- //Program to print two table
- main()
- {
- int i;
- for(i=1;i<=10;i++)
- printf("2 x %d = %d\n",i,2*i);
- return 0;
- }
The output of the above program is