OCJP

For loop in C Language

As we have discussed earlier, loop are used for repeated statement executions. Same as while loop ‘For Loop’ is also an entry control loop. Means for loop checks condition at first, if condition is false then execution is stopped.

Syntax

for(var = value; condition ; change in variable)
{
}
#include<stdio.h>
#include<conio.h>
void main()
{
	int i;
	clrscr();
	for(i=100;i<=200;i+=10)
	{
	printf("%d ",i);
	}

getch();
}
//Output will be : 100 110 120 130 140 150 160 170 180 190 200

We can have different formats of for loop like:

int x=1;
for(;x<=10;x++)
{
printf("%d ",x);
}
int x=1;
for(;x<=10;)
{
printf("%d ",x);
x++;
}
int x=1;
for(;;)
{
printf("%d ",x);
x++;

if(x>10)
break;

}

In last example loop will be break when value is >10.

For loop can be used with any data type like

char ch;
for(ch='A';ch<='Z';ch++)
{
printf("%c ",ch);
}

Leave a Reply

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


× How can I help you?