We can use one or more for loops in between one for loop.This is called nested for loops.Sometime it become very very important to use nested for loop.
For loop variations :
We can initialize more than one variable in to for lop.Like :
for( i=0, j=0; i<20; i++ )
for( p=1,q=1; p+q<20; p++,q++ )
We can also initialize variables outside of for loop.Like :
i=0 //initialization
for( ; i<20; i++ )
If we need to use increment/decrements outside of for loop than we can do like this :
i=0
for(;i<20;)
{
printf("%d",i);
i++; //increment
}
We can use logical operators too in between conditions like :
j=0;
for ( i=0; i<10 && j!90; i++ )
Infinite for loop :
If we need to create an infinite loop we can use for.To create this infinite loop we cannot use initialization,condition and increment into for.Like :
for( ; ; )
{
printf("\n finite loop \n");
}
After running this program press Ctrl - Break at a time.
Post a Comment