What does exactly a double while loop do??

I have a question that puzzles me. I think I understand the while loop on a single variable, but I am confused by while loops having two conditions like in this fragment of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
     int m = s1.length(), n = s2.length();
 
      if (abs(m - n) > 1)
        return false;
 
       int count = 0; // Count of edits
 
        int i = 0, j = 0;
        while (i < m && j < n)
         {
        
          if (s1[i] != s2[j])
         {
            if (count == 1)
                return false;


My quesiton is: in what order exactly does the double while loop operates?
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), (0,1), (0,2), ... etc? Or what else? This really confuses me.
Double while loop? Do you mean nested while loop? A while block inside a while block? The only thing I see is a double condition in the while loop snippet, the && means AND;

while(a < 10 AND(&&) b > 11) c = 10;

You can also use || for OR, e.g - if(this || that)

Let me know if that's what your after. Sorry for not using code tags I'm only phone!

Edit: sorry didn't read your question thoroughly the double condition thing is what you want. I do believe it is equated left to right but still follows parenthesis rules (I think):

while(cond1 && (cond2 || cond3)) - I believe condition 2 will be evaluated then condition 3 then condition1 (a more versed member will know this). Rule of thumb would be if 2 or more conditions rely on each other to form the result stick them in parenthesis.
Last edited on
Hi megatron 0 thank you for your answer, but I really didn't get anything of what you say... still puzzled by this double while loop :(
Your 'double while loop' is a loop with two conditions, linked by an '&&' (or 'AND').

In this case, it means that both the first condition and the second condition must be fulfilled for the loop to occur.

For example:
1
2
3
4
5
6
7
8
9
10
int i = 0;
int j = 0;
// this will loop while i is smaller than 10 AND j is smaller than 5
while (i < 10 && j < 5) {
    i = i + 1;
    j = j + 1;
}
// at this point, both i and j are 5;
// even though i is still smaller than 10, because j is no longer
// smaller than 5 the loop finished.  


As for your original question, in which order is the loop executed... I have no idea. Your code fragment doesn't show how i and j are updated. It could be like my example above, where both are incrementing simultaneously, but it could be anything.

EDIT:
Also, there's no guarantee a 'normal' while loop will iterate sequentially, either. Take a look at this one:
1
2
3
4
5
int i = 1;
while (i < 1000) {
    i *= 2;
}
// this loop will only run 10 times, and will finish with i=1024 
Last edited on
Hi Twilight Spectre,

thank you for your answer. I will post the whole code: may you see if you understand better the funcionting of this doube while loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
      // Returns true if edit distance between s1 and
      // s2 is one, else false
      bool isEditDistanceOne(string s1, string s2)
      {
        // Find lengths of given strings
        int m = s1.length(), n = s2.length();
 
        // If difference between lengths is more than
        // 1, then strings can't be at one distance
        if (abs(m - n) > 1)
        return false;
 
          int count = 0; // Count of edits
 
         int i = 0, j = 0;
          while (i < m && j < n)
         {
             // If current characters don't match
             if (s1[i] != s2[j])
           {
            if (count == 1)
                return false;
 
            // If length of one string is
            // more, then only possible edit
            // is to remove a character
            if (m > n)
                i++;
            else if (m< n)
                j++;
            else //Iflengths of both strings is same
            {
                i++;
                j++;
            }
             
            // Increment count of edits 
            count++;
          }
 
            else // If current characters match
           {
            i++;
            j++;
           }
          }
 
         // If last character is extra in any string
        if (i < m || j < n)
          count++;
 
         return count == 1;
      }
Last edited on
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.
Last edited on
Hi dhyaden. Yes, I see where I was making a mistake: the while loop advances *according to the condition*, and not sequentially as I believed, which is something that only the for loop does. Thanks for your illuminating post!
sequentially as I believed, which is something that only the for loop does

Wrong again. The for loop is very generalized. for (initial; test; increment) statement;
is equivalent to
1
2
3
4
5
6
7
{
    initial;
    while (test) {
        statement;
        increment;
    }
}

[Well not exactly equivalent. a continue in the for loop will still execute increment.]

You can put (almost?) anything in initial and increment. A common example that has nothing to do with counters is walking through the nodes of a list:
for (Node *tmp=head; tmp != nullptr; tmp = tmp->next) ...
Topic archived. No new replies allowed.