Reputation: 177
I have a question for the value of j
inside this nested loop.
for (potentialSum=1; potentialSum<=m; potentialSum ++)
{
for (j=1;j<=n;j++)
{
if (potentialSum == 2) {
printf("j:%d in loop\n", j);
}
}
C[potentialSum]=(j<=n) ? j : (-1);
if (C[potentialSum] == -1) {
printf("j:%d n:%d \n", j , n);
}
}
n = 0 and m = 25.
So when I run this loop with the aforementioned values for n and m, I get an output something like this:
j:1 in loop
j:2 in loop
j:3 in loop
j:4 in loop
j:5 in loop
j:6 in loop
j:7 in loop
j:8 n:7 // Outside of loop
My question is when/how does j
get incremented to 8, if n=7
?
This only happens when potentialSum = 2
, for the complete code click here and for a copy of the input click here.
Thanks for all the help in advance, I'm just really not seeing how j
goes from 7 to 8 outside of the loop.
Upvotes: 1
Views: 811
Reputation: 4395
for (j=1;j<=n;j++) //where n is 7
for( declaration ; comparison(condition checking) , increment/decrement)
after declaration, value is compared, and at the end its incrementing (j++
)
when j=7
it will check condition j<=n
which is true so it will go inside the loop. and at the it will increment j++
.
Now current value of j
will become 8
. Next time it will check condition j<=n
which is false so it will come out of the loop, but j
will remain 8
.
Upvotes: 5