So is the first statement executed until it evaluates to false and then goes to the second one or does the first loop execute, then the second one and goes back to first one? |
Why would it go back to the first one?
The first statement is a loop for j over values of 1 to i. It executes entirely.
Then, the second statement executes entirely. It is a loop for j over values of i to 5.
Statements don't magically interrupt each other. Each statement executes in turn. (*) It's not going to perform a bit of the first, then a bit of the second, then a bit of the first, etc.
It's all logical. One statement follows another.
I'm also confused on whether the values of i and j are reset to values in the first condition of the for loop....like for(j=1;...;...).....if j wasn't = 1, is it reset to a value of 1 when there is a pre-increment (++j)?
|
It sounds as though you don't understand quite what the syntax of a for loop means. The first part happens once, at the start of a loop. So, at the start of the first loop, j is set to 1. At the start of the second loop, j is set to i.
The third part happens at the end of each iteration of the loop, before the next check of the condition. It doesn't have anything to do with the initial condition of the loop.
So, at the start of the 1st iteration of the 1st loop, j (which is 1) is compared to i. If it's less than i, an iteration of the loop is performed. Then, at the end of that iteration, j is pre-incremented, so that it is now 2. It is then compared to i again. If it is still less than i, another iteration occurs, and then j is incremented again. And so on, until it fails the test of being less than i, at which point that loop stops.
Then the next loop begins. At the start, j is set equal to i. Then it is checked to see if it's less than 5. If so, a loop iteration is performed. At the end of the iteration, j is pre-incremented, and the condition is checked again.
Now, where in all this is the value of i changed? Look at the code, and tell me, logically, where you think the value of i changes.
(*) Well, not within a single thread, anyway. But I think threading is beyond the scope of this discussion :)