For Loop

Basically a For loop looks like this

for( 1st block ;2nd block; 3rd block)
//some codes here

A For loop contains 3 part.

1st block [1st iteration expression(s)]

The name of the first block is 1st iteration expression(s). It means this part will execute only for first loop. This part will not be necessary or will not execute for rest of the loop. Most of the Tutorial ,blog even wiki call this block as  initialization. Most of the programmer do initialization here that’s why this people call this part initialization. But this part only execute for first loop. So we can write any code here not only initialization. Even we can keep blank. Here are some examples what we can write at first block:
1. blank
2. i=0; //initialization
3. i=0,j=0,k=0; //multiple initialization
4. a=i;
5. a=(i==5?i:0);
6. i++;//increment or decrement
7. a=i++,j++;

We can write any expression separating with ,(comma).

*as we can keep this part blank so its not mandatory.

2nd block[ Condition]

The name of second block is  Condition . A for Loop will continue till this part’s statement is true. Many programmer not only use true/false alos use 0,1 or other value.This depends on programming language how they treat those values as true/false. But this value of this part must be true/false. We can also keep this part blank.Normally blank means  false but at for loop  if we keep blank at this block it means it is true. So it will become an infinity loop. We can use a break condition at code block to avoid infinite loop.

*as we can keep this part blank so this is also not mandatory.

3rd block [others iteration expression(s)]

The name of third part is others iteration expression(s). That means this part will execute  for all loop except the first time. Most of the Tutorial ,blog even wiki called this part as  Increment/Decrement.  Most of the programmer do Increment/Decrement here that’s why they call it Increment/Decrement. But it is same as 1st block.But deference is 1st executes first time and this one execute rest of the loop except first one.


Codes block part will execute each loop until condition part is true. When condition part false for loop ends.

Notice: for(;;); is a valid for loop.but it is infinity loop.

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *