The for
statement allows for a controlled loop. Its is also an entry-controlled loop (like while) in which first the condition is checked, and if it is True then the statements are executed. However, if the condition is False, the control will be transferred to the statement immediately after the body of loop. The condition can be any
constant, variable or expression that evaluates to True or False.
for (Expression 1; Expression 2; Expression 3) { //code to be executed }
Its behaviour is equivalent to a while loop with the following arrangement of expressions.
expression1; while (expression2) { statement; expression3; }
In the for loop, expressions 1, 2, and 3 are optional, although the semicolons must remain. If expressions 1 or 3 are not there, then the loop simple behaves like the while loop above without expressions 1 or 3. If expression 2 is omitted, then the conditional is always TRUE, and an infinite loop results.
Let's see the simple program of for loop that prints table of 1.
#include<stdio.h> int main() { int i = 0; for (i = 1; i <= 10; i++) { printf("%d \n", i); } return 0; }
#include <stdio.h> int main() { int i = 1, number = 0; printf("Enter a number: "); scanf("%d", & amp; number); for (i = 1; i <= 10; i++) { printf("%d \n", (number * i)); } return 0; }
The expression represents the initialization of the loop variable. We can initialize more than one variable in Expression 1. Expression 1 is optional.
Example 1
#include <stdio.h> int main() { int a,b,c; for(a=0,b=12,c=23;a<2;a++) { printf("%d ",a+b+c); } }
Example 2
#include <stdio.h> int main() { int i=1; for(;i<5;i++) { printf("%d ",i); } }
Output
Expression 2 is a conditional expression. It is optional. It checks for a specific condition to be satisfied. If it is not, the loop is terminated. It can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements. It can perform the task of expression 1 and expression 3. So we can initialize the variable as well as update the loop variable in expression 2 itself. We can pass zero or non-zero value in expression 2. However, in C, any non-zero value is true, and zero is false by default.
Example 1
#include <stdio.h> int main() { int i; for(i=0;i<=4;i++) { printf("%d ",i); } }
Example 2
#include <stdio.h> int main() { int i,j,k; for(i=0,j=0,k=0;i<4,k<8,j<10;i++) { printf("%d %d %d\n",i,j,k); j+=2; k+=3; } }
Output
Example 3
#include <stdio.h> int main() { int i; for(i=0;;i++) { printf("%d",i); } }
Output
Expression 3 is used to update the loop variable. It is optional. We can update more than one variable at the same time.
#include<stdio.h> void main () { int i=0,j=2; for(i = 0;i<5;i++,j=j+2) { printf("%d %d\n",i,j); } }
0 2 1 4 2 6 3 8 4 10
The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don't need to use braces. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside.
#include<stdio.h> void main () { int i; for(i=0;i<10;i++) { int i = 20; printf("%d ",i); } }
If expression 2 is omitted, then the conditional is always TRUE, and an infinite loop results. To make a for loop infinite no need to give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop.
for (;;) /* infinite loop */ statement;
#include<stdio.h> void main () { for(;;) { printf("welcome to rookinerd"); } }
If you run this program, you will see above statement infinite times.