Convert infinite for loop to final for loop

in C, how can we convert an infinite loop to a finite loop without repelling anything in the for loop syntax ....

#include<stdio.h>
#include<conio.h>
int main()
{
    int a;
    a=1;
    a++;
    for( ; ; )
    {
       a<=10;
       printf("%d",a); 
    }
    getch();
}
+3
source share
6 answers

Here you can use the instructions break.

This will exit the loop and start control under the loop body.

+8
source
#include<stdio.h>
#include<conio.h>

int main()
{
    int a = 0;
    for(;;)
       if ((++a) <= 10)
         printf("%d",a);
       else
         break;
    getch();
}
+2
source

I think this is what you ask here ...

EDIT

int main()
{
   int a;
   a=0;
   for(;;)
   {
        if(a>10)
        break;
        printf("%d",a);
        a++
   }
   getch();
}
+1
source

Make a condition inside the loop where you want to end it. Otherwise, use break or exit as statements ...

0
source

try this code:

#include<stdio.h>
#include<conio.h>
int main()
{
    int a;
    for(a=1 ; a<=10; a++)
    {
         printf("%d",a); 
    }
    getch();
}
0
source
#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();
    }
0
source

All Articles