- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
For Loop in C Programming 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
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);
}
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(); }
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:
- At first value of “i” is set to 1, this happens only once in loop execution.
- Next loop condition (i<=10) is tested. Since value of “i” is 1, it satisfies loop condition and loop statement is executed.
- 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++).
- 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).
- 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).
Reader's feedback
Did this help you to learn about for loop in C programming?
Useful C Programming links
- Loops in C Programming Language and Its Types
Loop is one of the important parts of C language and study of C language is incomplete without this. So let’s head towards completion of our knowledge about C language. As a dedicated C language leaner, right now you should have two questions. - How to work with While loop in C Programming
I already explained what is loop and why we use loop in c language, and I am not going to repeat that here again. So we will start with while loop explanation. While loop is one of the simple loop available in C language.It’s very easy to use, lets - How to work with Do-While loop in C Programming
If you are looking for what is loop and why we use loop in c language then you can check my article on that topic. This article is entirely dedicated to do-while loop and its usage. I must tell you that do-while loop is similar to while loop but only
© 2011 RAJKISHOR SAHU