for loop in C

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.

Syntax
snippet
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.

Flowchart of for loop in C

for loop in c language flowchart

C for loop Examples

Let's see the simple program of for loop that prints table of 1.

snippet
#include<stdio.h>
int main() {
    int i = 0;
    for (i = 1; i <= 10; i++) {
        printf("%d \n", i);
    }
    return 0;
}
Output
1 2 3 4 5 6 7 8 9 10

C Program: Print table for the given number using C for loop

snippet
#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;
}
Output
Enter a number: 2 2 4 6 8 10 12 14 16 18 20
Output
Enter a number: 1000 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
Expression 1

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

snippet
#include <stdio.h>
int main()
{
    int a,b,c;
    for(a=0,b=12,c=23;a<2;a++)
    {
        printf("%d ",a+b+c);
    }
}
Output
35 36

Example 2

snippet
#include <stdio.h>
int main()
{
    int i=1;
    for(;i<5;i++)
    {
        printf("%d ",i);
    }
}

Output

Output
1 2 3 4
Expression 2

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

snippet
#include <stdio.h>
int main()
{
    int i;
    for(i=0;i<=4;i++)
    {
        printf("%d ",i);
    }
}
Output
0 1 2 3 4

Example 2

snippet
#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

Output
0 0 0 1 2 3 2 4 6 3 6 9 4 8 12

Example 3

snippet
#include <stdio.h>
int main()
{
    int i;
    for(i=0;;i++)
    {
        printf("%d",i);
    }
}

Output

Output
infinite loop
Expression 3

Expression 3 is used to update the loop variable. It is optional. We can update more than one variable at the same time.

snippet
#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    
Loop body

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.

snippet
#include<stdio.h>
void main ()
{
    int i;
    for(i=0;i<10;i++)
    {
        int i = 20;
        printf("%d ",i);
    }
}
Output
20 20 20 20 20 20 20 20 20 20

Infinitive for loop in C

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.

Syntax
for (;;) /* infinite loop */
    statement;
snippet
#include<stdio.h>
void main ()
{
    for(;;)
    {
        printf("welcome to rookinerd");
    }
}

If you run this program, you will see above statement infinite times.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +