ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

For Loop in C Language with Example

Updated on August 30, 2014

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


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


void main()

int i;

	printf("%d for loop.\n",i);
For loop example output
For loop example output | Source


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


    0 of 8192 characters used
    Post Comment

    • rajkishor09 profile image

      RAJKISHOR SAHU 3 years ago from Bangalore, Karnataka, INDIA

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

    • profile image

      usama 3 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++).

    • profile image

      jj 4 years ago


    • profile image

      p.srinu 4 years ago

      good explanation and simple example

    • profile image

      Sharjeel Rehman 5 years ago

      Thanks Sir.. :)

    • profile image

      sdnms 5 years ago

      good example\

    • profile image

      SURE 5 years ago

      Its really nice to remember everything in C.

    • profile image

      virendar kumar 6 years ago

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

    • profile image

      deep 6 years ago

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

    • profile image

      ANANT 6 years ago


    • vinner profile image

      vinner 6 years ago from India

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

    • rajkishor09 profile image

      RAJKISHOR SAHU 6 years ago from Bangalore, Karnataka, INDIA

      @s.paswan : you can read that here.

    • profile image

      s.paswan 6 years ago

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

    • rajkishor09 profile image

      RAJKISHOR SAHU 6 years ago from Bangalore, Karnataka, INDIA

      @youngbesttips : thank you...

    • youngbesttips profile image

      youngbesttips 6 years ago from United Kingdom

      This C tutor is cool