Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Loops

C Programming Language Loop

In computer programming, looping is the process of executing the same thing again & again.

In C programming, loops are used when there is a need to execute some specific part of the program repeatedly.

Assume that there is a need to print our individual name 20 times repeatedly then it is very troublesome to write it 20 times in a program. The only way to avoid this type of programming approach is to use loops. Using loops will help us to write name only 1 time and printing it 20 times.

There are three types of loops in C programming:
    i) for loop
   ii) while loop
   iii) do-while loop

The for loop

Syntax

for(initialization;condition;update)
{
    // programming
    // statements
}

The for loop consists of three expressions called initialization, condition & update and its body (statements inside curly braces).

Flowchart

C for Loop flowchart

Working

C Programming Language for Loop Working

Step I –
         Program control executes initialization expression.

Step II –
         After initialization, the condition is checked.

Step III –
         If the condition is true, the body of for loop is executed.

Step IV –
         After execution of for loop body, program control jumps to update expression.

Step V –
          After execution of update expression program control again returns to condition expression to check whether the condition becomes true or false.

If the condition is true again Step-III, Step IV & Step V are repeated.

This process goes on till condition does not become false.

Example No 1

#include<stdio.h>
int main()
{
    int i;
    for(i=1;i<=10;i++)
    {
        printf("\n John");
    }
    return 0;
}

Output

 John
 John
 John
 John
 John
 John
 John
 John
 John
 John

Thus with the help of for loop in the above program, we have succeeded to print name 10 times by writing it just 1 time.

Example No 2

#include <stdio.h>
int main()
{
    int n;
    for(n=1;n<=10;n++)
    {
        printf(" %d",n);
    }
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

Here we have succeeded to print numbers 1 to 10 by writing just single line

printf(" %d",n);

in the for loop.

Is it right?

Case – 1

for( ;condition;update)
{
    // programming
    // statements
}

Yes of course! We can avoid writing expressions in the for loop.

Example

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

Output

1 2 3 4 5 6 7 8 9 10

Case – 2

for(initialization;  ;update)
{
    // programming
    // statements
}

Example

#include<stdio.h>
int main()
{
    int i;
	
    for(i=1; ;i++)
    {
        if(i<=10)   //condition
             printf(" %d",i);
        else
             break;	
    }
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

Case – 3

for(initialization;condition; )
{
    // programming
    // statements
}

Example

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

Output

1 2 3 4 5 6 7 8 9 10

Case – 4

for(   ;   ;   )
{
    // programming
    // statements
}

Example

#include<stdio.h>
int main()
{
    int i;
    i=1;       //Initialization
    for( ; ; )
    {
        if(i<=10)      //condition
              printf(" %d",i);
        else
              break;
		    
        i++;	 //Update
    }
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

 

Leave a reply