Flag This Hub

For Loop in C

By


Source: c language

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
See all 2 photos
For loop example
Source: Author

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).

C Programming Language (2nd Edition)
Amazon Price: $35.64
List Price: $67.00
C (Vintage)
Amazon Price: $1.03
List Price: $15.00
Programming in C (3rd Edition)
Amazon Price: $24.00
List Price: $49.99
C Primer Plus (5th Edition)
Amazon Price: $30.63
List Price: $54.99

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: Author

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” and where 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

Comments

youngbesttips 5 months ago

This C tutor is cool

rajkishor09 5 months ago

@youngbesttips : thank you...

s.paswan 5 months ago

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

rajkishor09 5 months ago

@s.paswan : you can read that here. http://rajkishor09.hubpages.com/_eknow/hub/Loop-in

vinner 5 months ago

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

ANANT 4 months ago

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

deep 2 months ago

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

virendar kumar 7 weeks ago

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

SURE 2 weeks ago

Its really nice to remember everything in C.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working