For Loop in C Language with Example

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?

  • Yes
  • No
  • Can't say
See results without voting

More by this Author


Comments 15 comments

youngbesttips profile image

youngbesttips 4 years ago from United Kingdom

This C tutor is cool


rajkishor09 profile image

rajkishor09 4 years ago from Bangalore, Karnataka, INDIA Author

@youngbesttips : thank you...


s.paswan 4 years ago

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


rajkishor09 profile image

rajkishor09 4 years ago from Bangalore, Karnataka, INDIA Author

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


vinner profile image

vinner 4 years ago from India

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


ANANT 4 years ago

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


deep 4 years ago

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


virendar kumar 4 years ago

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


SURE 4 years ago

Its really nice to remember everything in C.


sdnms 3 years ago

good example\


Sharjeel Rehman 3 years ago

Thanks Sir.. :)


p.srinu 3 years ago

good explanation and simple example


jj 3 years ago

tnx.....:)


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

rajkishor09 2 years ago from Bangalore, Karnataka, INDIA Author

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

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working