c
C Programming Tutorial : Introduction

C Loops: For Loop With Example

The for loop is extremely useful while programming in C. It has three expressions and semicolonsare used for separating these expressions. The ‘for’ loop can be written as-

Syntax -1

for(expressionl; expression2; expression3)
statement;
 

Syntax -2

for(expressionl;expression2;expression3)
{
    statement;
    statement;
    ……………….
    ……………….
}
 
For-Loop flow chart

The loop body can be a single statement or block of statements.

Expression-1 is an initialization expression, expression-2 is a test expression or condition and expression-3 is an update expression.

  1. Expression-1 is executed only once when the loop starts and is used to initialize the loop variables. This expression is generally an assignment expression.
  2. Expression-2 is a condition and it is tested before each iteration of the loop. This condition generally uses relational and logical
  3. Expression-3 is an update expression and is executed each time after the body of the loop is executed.

Now let us see how this loop works.

  1. Firstly, the initialization expression is executed, and the loop variables are initialized, and then the condition is checked
  2. If the condition is true, then the body of loop is executed.
  3. After, executing the loop body, control transfers to expression-3 (update expression) and it modifies the loop variables and then again, the condition is checked, and if it is true, the body of loop is executed.

This process continues till the condition is true and when the condition becomes false the loop is terminated and control is transferred to the statement following the loop.

The work done by the for loop can be performed by writing a while loop as-

Syntax-1

expression 1;
while(expre:ssion 2)
{
statement.
…………..
…………..
expression 3;
}
 

Although the task of while and for loops is same, the for loop is generally used when the number of iterations are known in advance and while loop is used where number of iterations are not known,

Example-1

I*Program to print the numbers from 1 to 10 using for loop*/
#include<stdio.h>
#include<conio.h>

void main( )
{
    int i;
    for(i=1;i<=10;i++)
    printf("%d\t”,i);
    printf ("\n”);
    getch();
}

Output:·
1 2 3 4 5 6 7 8 9 10
 

Example-2

/*Program to print numbers in reverse order with a difference of 2 */
#include<stdio.h>
#include<conio.h>

void main()
{
    int k;
    for(k=10;k>=2;k=k-2)
    printf("%d\tH,k) ;
    printf("\n”);
    getch();
}
Output:
10 8 6 4 2
 

Example-3

/* Multiply two positive numbers without using * operator*/
#include<stdio. h>
#include<conio. h>

void main( )
{
    int a,b,i;
    int resul t=0;
    printf("Enter two numbers to be multiplied ");
    scanf("%d%d”,&a,&b);
    for(i=1;i<=b;i++)
    result=result+a;
    printf("%d * %d =%d\n”,a,b,result);
    getch();
}
 

Example-4

/* Find the sum of this series up to n terms 1+2+4+7+11+16+ */
#include<stdio.h>
#include<conio.h>

void main ( )
{
    int i, n, sum=0, term=1;
    printf ("Enter number of terms ") ;
    scanf ("%d" ,&n) ;
    for(i=1;i<=n;i++)
        {
            sum+=term;
            term=term+i;
        }
        printf ("The sum of series upto %d terms is %d\n", n, sum);
    getch();
}
 

Example-5

/*Program to generate Fibonacci series 1,1,2,3,5,8,13,34,55,89 In
this series each number is a sum of the previous two numbers*/

#include<stdio.h>
#include<conio.h>

void main( )
{
    long x, y,z;
    int i,n;
    x=0;
    y=1;
    printf ("Enter the number of terms ");
    scanf ("%d",&n) ;
    printf("%ld”,y);
    for(i=1;i<n;i++)
        {
            z=x+y;
            printf(“%ld”,z);
            x=y;
            y=z;
        }
    printf("\n") ;
    getch();
}
 

All the three expressions of the for loop are optional. We can omit anyone or all the three expressions in the for loop but in any case, the two separating semicolons should always be present.

  1. Expression-1 is omitted when the initialization work is done before entering the loop.
  2. Expression-2 is a condition and if omitted, it is always assumed to be true and so. this type of loop will never stop executing. This type of loop is infinite and to avoid it there should be a statement inside the loop that takes the control out of the loop.
  3. Expression-3 is an update expression and is omitted when it is present inside the body of the loop.

Example-6

/*Program to print the sum of digits -of any number using for loop */
#include<stdio.h>
#include<conio.h>
void main ()
{
    int n, sum=0, rem;
    printf ("Enter the number ");
    scanf ("%d" ,&n) ;
    for ( ; n>0 ;n/=10)
        {
            rem=n%10; /*taking last digit of number*/
            sum+=rem;
        }
    printf ("Sum of digits = %d\n", sum) ;
    getch();
}
 

We can also have any number of expressions separated by commas. For example, we may want to initialize more than one variable or take more than one variable as loop variable.

Example-7

/ *P5. 22 Program to print numbers using for loop* /
#include<stdio.h>
#include<conio.h>
void main ( )
    {
    int i, j ;
    for(i=0;j=10;i<=j;i++,j--)

    printf("i = %d j= %d\n",i,j);
    getch();
    }
Output:
i = 0	j = 10
i = 1	j = 9
i = 2	j = 8
i = 3	j = 7
i = 4	j = 6
i = 5	j = 5