For Loops ‘For loops’ are more concise and special kind of loops that are used to execute statements repeatedly in some programming languages including C. The For loop structure is quite different from while loops as well as do while loop structures which we had a look in previous articles. ‘For loops’ are usually used when we need to execute specific number of iterations i.e when number of iterations is known before entering the loop. It follows the following format:- Code: for(initialization ; condition ; afterEachExecution ) { CodeHere; } The Working First the program runs the code or an assignment specified in the ‘initialization’ section. Then it checks whether the condition specified evaluates to true If ‘yes’ then the loop body is executed. Then after executing the program runs the code or an assignment specified in the ‘afterEachExecution’ section Then after executing the program again checks if the condition evaluates to true If it is true the loop body is executed This process of repeatedly execution continues until the condition evaluates to false. That’s much of a theory there now let the coding begin. Using for loops Now let’s create a simple C program which would basically print all the even numbers between 1 and 100. Code: #include<stdio.h> int main() { int i=1; for(i = 2 ; i < 100 ; i = i+2 ) { printf("%d\n",i); } return(0); } Explanation:- First we declare a ‘int’ variable named ‘i’ . ‘i’ is used to as a loop counter and to display even numbers on the screen. In the loop initialization, we set ‘i’ to 2 (first even number). Then the program checks if the condition specified in the condition section evaluates to true. Then the program moves to loop body where we output ‘i’ (our even number) on to the screen. Then the program jumps to the ‘afterEachExecution’ where it increments ‘i’ by 2. This procedure (from step 3) continues until the condition specified in the condition section evaluates to false. Output:- Code: 2 4 6 8 10 12 14 16 18 20 22 24 26 ------------------------- Snipped ------------------------------ That’s all for this tutorial stay tuned for more.