ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

For Loop in C Language with Example

Updated on August 30, 2014
Source

Along with while loop and do-while loop, for loop is also part of C programming language. For many programmers, for loop seems to be easiest loop comparing other two loops (while and do-while loop). Mainly its structure (syntactically) makes it easy to use. Its easy syntax made it popular. Let’s take a look at for loop syntax.



For loop syntax in C

Syntax:

for(<loop variable initialization>;<loop condition>;<loop variable increment/decrement>)

{

loop statement 1;

loop statement 2;

loop statement 3;

………………….

loop statement N;

}

In while loop and do-while loop we need to initialize loop counter variable before we use it in loop and we can increment/decrement loop counter variable inside loop block where sometimes programmer forgets to do that. But in case of “for loop”, it gives you option to provide all those statement in one line (not mandatory, you can use while and do-while style if you prefer that style).

Example :

for(i=1; i<=10; i++)

{

printf("%d : for loop.\n", i);

}

For loop example
For loop example | Source

In above image as you can see, we have for loop. It starts with “for” keyword and in bracket we have provide initialization, condition and increment statement.

If you want to use for loop in your program then like other loop three condition must be satisfied.

  • A loop counter variable (here “i” is loop counter).
  • Condition to execute loop statements (“(i<=10)” is condition).
  • Increment or decrement of loop variable (i++ is incrementing value of “i” by 1).

For loop example

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

void main()
{

int i;
clrscr();

for(i=1;i<=10;i++)
{
	printf("%d for loop.\n",i);
}
getch();
}
For loop example output
For loop example output | Source

Explanation

A very simple program that prints simple message, see below output image. I have used same example for all loop so that it will be easy for you to compare for loop with while and do-while loops.

When above “for loop” block (line no. 10 - 13) gets executed let me tell you what happens.

For loop execution steps:

  1. At first value of “i” is set to 1, this happens only once in loop execution.
  2. Next loop condition (i<=10) is tested. Since value of “i” is 1, it satisfies loop condition and loop statement is executed.
  3. When we reach at closing brace of “for loop” then control moves back to beginning of “for loop variable” and value of “i” is incremented by 1 (i++).
  4. Again it starts from step no. 2. This looping will continue till loop condition (i<=10) is tested to false (means condition is no longer true because value of “i” would be greater than 10, i.e. 11).
  5. When value of “i” reaches to 11 then control exits from loop and next statement, after loop block, is executed (in above program line no. 14).

Tell you opinion about for loop.

Did this help you to learn about for loop in C programming?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • youngbesttips profile image

      youngbesttips 5 years ago from United Kingdom

      This C tutor is cool

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

      @youngbesttips : thank you...

    • profile image

      s.paswan 5 years ago

      please tell me that where we can use for loop and why

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

      @s.paswan : you can read that here. https://hubpages.com/technology/Loop-in-C-language...

    • vinner profile image

      vinner 5 years ago from India

      Very nice work friend. Very simple tutorials explaining complex things. good work, voting up

    • profile image

      ANANT 5 years ago

      THANKS...ITS VERY SIMPLE EXPLANATION...AND GOD TO UNDERSTAND...

    • profile image

      deep 5 years ago

      Thanks sir it is very simple..............................

    • profile image

      virendar kumar 4 years ago

      thank sir it is best suited for me learning about the loops but please discribe here about the functions

    • profile image

      SURE 4 years ago

      Its really nice to remember everything in C.

    • profile image

      sdnms 4 years ago

      good example\

    • profile image

      Sharjeel Rehman 4 years ago

      Thanks Sir.. :)

    • profile image

      p.srinu 3 years ago

      good explanation and simple example

    • profile image

      jj 3 years ago

      tnx.....:)

    • profile image

      usama 2 years ago

      there is mistake in explanation

      3. When we reach at closing brace of “for loop” then control moves back to beginning of “for loop” and where value of “i” is incremented by 1 (i++).

      it has to be

      3. after execute of statement control moves "loop variable increment/decrement" and where value of “i” is incremented by 1 (i++).

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 2 years ago from Bangalore, Karnataka, INDIA

      @Usama : thank you for valuable feedback. I have updated the statement to more meaningful.

    Click to Rate This Article