Conversion of infinite for loop to finite for loop
in C,how can we convert an in开发者_如何学Pythonfinite loop into finite loop without wrinting anything in syntax of for loop....
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
a=1;
a++;
for( ; ; )
{
a<=10;
printf("%d",a);
}
getch();
}
You could use break
statement there.
This will exit the loop and start control beneath the loop body.
#include<stdio.h>
#include<conio.h>
int main()
{
int a = 0;
for(;;)
if ((++a) <= 10)
printf("%d",a);
else
break;
getch();
}
I guess this is what you are asking here...
EDIT
int main()
{
int a;
a=0;
for(;;)
{
if(a>10)
break;
printf("%d",a);
a++
}
getch();
}
Make a condition inside the loop where you want to end that.. Otherwise use break or exit like statements...
try this code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
for(a=1 ; a<=10; a++)
{
printf("%d",a);
}
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
a=1;
m:
for(;;)
{
if(a<=10)
{
printf("%d\n",a);
a++;
}
if(a<10)
{
goto m;
}
}
getch();
}
精彩评论