Help me walk through a few loops to see how many executions. I need a lesson
Posted on 2004-09-25
I'm having difficulty finding out how many executions these loops operate sum++. i'm not only interested in the answer but how youy figured it out.
A) for(i=1 ; i<= 100 ; i++)
for (j= 1 ; j <= i; j++) sum++;
A) i'm thinking that the first for loop goes through 100 times and the second for loop is also 100 times so 100^2 is correct?? is this right?
B) for(i = 101; i <=200; i++)
for(j= i - 1; j <i+1;j++) sum++;
B) I tried walking through it and it seems the first loop starts out and goes through 99 times. hen thew second one is true for 99 times so 99^2
my thinking was as follows
1st time through j =100 ; 100 < 101 +1; 100++
2nd time j=101 ; 101 <102+1; 101++
199th time j= 199 ; 199 < 200 +1 ; 200++ so stops here 199-100 = 99 times....is this the right logic??