That is, if I have a loop on a single iterator i, I know that the condition inside the body is checked sequentially, i.e. i=0, 1, 2,... But in this case? IN which order precisely is the loop executed? Is it something like (i,j)= (0,0), |
The
while
loop itself does
not define how the variables in the condition are modified. So your statement about a single iterator i is not true. The body of the loop can modify
i
in any way it wants. For example:
1 2 3 4 5 6
|
// i goes 0, 2, 4, 6, ...
int i=0;
while (i<10) {
do_stuff();
i += 2;
}
|
1 2 3 4 5 6
|
// i goes 10, 9, 8, ...
int i=10
while (i>0) {
do_stuff();
--i;
}
|
1 2 3 4 5 6
|
// i goes 1, 2, 4, 8, 16, ...
int i=1
while (i< 1000000) {
do_stuff();
i *= 2;
}
|
It's similar when the condition is more complex.
To put it another way, the
only thing that a while loop does is:
- evaluate the condition
- if the condition is true, then execute the body of the while loop
- else jump to the first statement after the while loop.
It's up to the body of the while loop to change the state of the program so that the condition will eventually be false. Exactly how this happens is up to the code in the body.
In your code, the loop increments i, or j, or both, depending on how s1[i] and s2[j] compare.