for statement in c language
for statement is a looping statement of c language. Looping statements are also known as iterative statements.
Looping statements are used to repeat same set of statements again and again depending upon a condition.
for statement is an entry controlled looping statement. It is basically used when we clearly know how many times the loop will execute.
In this statement, initialization, condition and updation are performed in same line. So it is very easy to use and understand. The syntax of for statement is:
for is a keyword which specifies that for statement has started.
Initialization is the way to store some initial value in the control variable. Control variable is the variable which is used to control how many times loop should execute.
Control variable can of any data type like int, float, char etc.
In this part, control variable is initialized with some value using assignment operator. It is basically used to specify the value from which the loop should start. We can also initialize more than one variable here.
If more than one variable have to be initialized, they should be separated by commas.
If condition is true, the loop will execute. The loop will continuously execute as long as condition is true.
Loop will terminate when the condition becomes false. In condition part, usually control variable is tested to check whether the loop should continue or not.
If condition is initially false, the loop will not execute even once. Multiple conditions can appear in this part but they should be combined with the help of logical operators.
i<=10, Age>=0 && Age<=18
Updation is the way to increment or decrement the value of a control variable. Updation can be done by using increment/decrement operator, shorthand operators or arithmetic operators.
Multiple updation statements can appear in this part depending upon the requirement.
If there are more than one updation statements, they should be separated by commas.
Statement-Block is the set of instructions which will execute as a part of for statement. Statement block can contain one or more statements. These instructions will repeatedly execute as long as condition is true.
Statement-X may be set of one or more statements which will be executed after completion of for statement.
i=1 is the initialization.
i<=5 is the condition.
i++ is the updation.
For statement can be explained through the following flowchart:
|Program to demonstrate the use of for statement.|
Description of above program
In the above program, initially variable i has been assigned value 1. Value of variable i is compared with 5 to check whether it is less than 5 or not.
If it is less than five, next statement i.e printf(“\n%d”,i); would be executed displaying value of i which is initially 1.
After executing this statement, control would go to updation statement i.e. i++which would increment the value of i by 1. i now becomes 2.
Again condition i<=5 would be tested. If it is true, printf(“\n%d”,i); would be executed.
Again updation will be performed followed by condition. This process is continuously repeated as long as condition is true.
Nesting of for statement
Nesting of for statement means that one for statement is enclosed within another for statement.
Nested loops are basically used to create matrix.
Any looping statement can contain one or more loop statements in itself.
In case of a matrix, outer loop is used for counting rows and the internal loop is used for counting columns.
Syntax for nested for statement is:
Body of inner loop
|Program to demonstrate the use of nested for statement.|
for(i=1;i<=3;i++) /*Outer Loop*/
for(j=1;j<=3;j++) /*Inner loop*/
Description of above program
In the above program, for one value of outer loop, inner loop will execute completely
for i=1, j will execute complete i.e. 1,2,3.
for i=2, j will again execute complete i.e. 1,2,3.
Similarly for i=3, j will work complete i.e. 1,2,3.
Tips and tricks
The output of above loop would be 6 because loop would execute in same line because of semicolon after for statement. When condition becomes false then pointer would jump to printf() statement.
The output of above loop would result in infinite loop because there is no initialization, no condition and no updation.
The output of above loop would be 1 2 3 4 5
Initialization has been done before for statement, condition has been given inside for and updation has been provided inside printf().
Test Your Knowledge
1. Write a program to find factorial of a number entered by the user.
2. Write a program to print the following:
3. Write a program to print the following:
4. Write a program to find sum of series x1+x3+x5+x7+x9
5. Write a program to find sum of series 1+5+9+13+17
6. Write a program to get following output
7. Write a program to print all prime numbers between 1 and 100 using do while statement.
8. Write a program to print table of any number entered by the user by using do while statement..
Write the output of the following programs:
1. int main( )
int a = 0 ;
for ( ; a ; )
printf( “\nHello” ) ;
2. int main( )
int a = 1 ;
for ( ; a<=5 ; )
printf( “\n%d”,a++ ) ;
int main( )