Re: break statement in a for loop



a.mil@xxxxxxxxx wrote:
Ok, this was a bad fast example, I admit.
Therefore below another example that shows the point.


int main()
{
unsigned long i,j,k,l,n;

for (l=0;l<10000000;l++) {
j=0;
k=10;
n=231;

for (i=1;n>3;j+=8,k+=j,n-=2) {
if (k%n) continue;
i=0;
break;
}
}
return 0;
}


Run this without the break statement and with the break statement and
look at execution time. On my 1.5GHz P4 it takes 1.3s and 13.9s
respectively (!). My store on the factor 100 incorporates a lot more
code.

On my system, without the "break", it takes 0.001 seconds (according
to "time"). That's because without the break, the compiler realises
your program does nothing and your for loop is useless, so it removes
it altogether. How about an actual demonstration (a program that does
something useful, such as printing the result of your calculation,
whatever it is you're trying to calculate)?

.



Relevant Pages

  • Re: Dharaskars C questions ???
    ... instead of break statement in a switch. ... within loops. ... int main ...
    (comp.lang.c)
  • Re: break statement in a for loop
    ... Richard Heathfield schreef: ... Run this without the break statement and with the break statement and ... int main ...
    (comp.lang.c)
  • Re: break statement in a for loop
    ... Run this without the break statement and with the break statement and ... int main ... Richard Heathfield ... rjh at the above domain, ...
    (comp.lang.c)
  • Re: break statement in a for loop
    ... Ok, this was a bad fast example, I admit. ... int main ... Run this without the break statement and with the break statement and ... My store on the factor 100 incorporates a lot more ...
    (comp.lang.c)