How many times can we use for loop

Feb 3, 2010 at 12:58pm
How many times can we use for loop in c language.....
i heard that there is a limit for using 'FOR LOOP' in c language....

please clear my doubt.....
Feb 3, 2010 at 1:00pm
What do you mean with that?
Feb 3, 2010 at 1:25pm
means number of for loops..........

like this.....

for(i=0;i<x;i++)
{
//statement
}

for(j=0;j<x;j++)
{
//statement
}

for(k=0;k<x;k++)
{
//statement
}

for(l=0;l<x;l++)
{
//statement
}
.
.
.
.
.
.
N number of times...

like this how many FOR LOOPS can we use
Feb 3, 2010 at 1:32pm
What do you mean? You want to know if there's some kind of limit to the amount of for loops in a program?

There is no limit. You can have as many for loops as you want. By the way, if you're using C++ you can just do this:
1
2
3
for (int i = 0; i < j; i++);
/* i doesn't exist any more */
for (int i =0; i < j; i++);

so you don't have to use the whole alphabet.
Feb 3, 2010 at 2:21pm
closed account (S6k9GNh0)
I will go ahead an throw in that if you find yourself pushing the limits of the compiler for having to many for loops, there are definitely problems with the code you provided.
Last edited on Feb 3, 2010 at 2:21pm
Feb 3, 2010 at 2:45pm
ofcourse till now i too know that there was no limit for 'FOR LOOPS'...........

but my friend said that there was a limit of using number of for loops in c...

Feb 3, 2010 at 2:48pm
closed account (S6k9GNh0)
Ask your friend the source of his information and that he should read the C or C++ standard documentation.

EDIT: Actually, it's not really mentioned to a limit of the number of times a for-loop could be used (in the standard). I'm almost positive that they assume that it's limitless though. There would be absolutely no reason to place a limit on the amount.
Last edited on Feb 3, 2010 at 5:03pm
Feb 5, 2010 at 6:37pm
Maybe your friend meant four-loops. In which case the answer is... 4. ;^)

I suppose there must be SOME real-world limitation. But it would probably depend on the amount of memory in your PC and it would be ridiculously large.
Feb 5, 2010 at 8:27pm
Theoretically, there is no limit. But like cnoeval said, the more code you have, the larger your executable file gets (and you can't really optimize this yourself very much with C/C++ (which is why assembly is great, and why writing boot sector programs is a good learning experience)), and therefore the more disk space and memory it will use... not to mention how slow it would be to get through 400 for loops.
Feb 5, 2010 at 9:16pm
Well, technically it would be limited to the size of the data type of your counter..

(unless you create an infinite loop)
Feb 5, 2010 at 9:45pm
No, there is no theoretical limit. The boundary of the data type you use as a counter only limits the amount of iterations you can do (but it doesn't really, because it will just overflow and continue looping... e.g. for (int i = 1; i > 0; i++); will loop until the i overflows and becomes INT_MIN); the sky (and the hard disk) are the only limits to the amount of for loops you can have.
Topic archived. No new replies allowed.