-2

I have a question about iterating in a while loop. So let's say we have a vector with size 10 and elements are from 0-9. We have a for loop that just iterates once and within the for loop we have the while loop. I am getting two different results when I print the loop and I'm unsure why.

I understand the idea behind the first loop where we increment j in the body of the while and we don't enter the loop on the last iteration since j is 5 so we only print 0-4. The second loop is where I'm having issues with understanding. My logic is first we increment the pointer j to 1 so that's why we cout 1 instead of 0. But once j is 4 v[j++] = 5 and in the condition in the loop breaks when v[j++] = 5 so why do we cout 5 still? Any help is appreciated.

  vector<int> v(10);
  for(int i = 0; i < v.size(); i++){
    v[i] = i;
  }
  int j = 0;
  for(int i = 0; i < 1; i++){ // first loop
    while(v[j] != 5){
      cout << v[j]; //prints 0 1 2 3 4
      j++;
    }
  }
  for(int i = 0; i < 1; i++){ //second loop
    while(v[j++] != 5){
      cout << v[j]; //prints 1 2 3 4 5
    }
  }
EliteCoder123
  • 25
  • 1
  • 5
  • When `j` is equal to `4`, `j++` changes it to 5 but returns 4. This is how post-increment works. – paddy Jul 26 '21 at 05:05
  • Second one might be more clear as something like `while (v[j] != 5) {++j; cout << v[j];}` – mediocrevegetable1 Jul 26 '21 at 05:06
  • 1
    Just a minor point on language: _"first we increment the pointer j to 1"_... Note that `j` is _not_ a pointer -- it is an index. Be careful with the word "pointer" because it means something very specific. – paddy Jul 26 '21 at 05:09
  • You forget to reinit `j` at 0 between the 2 loops to have output from comments. – Jarod42 Jul 26 '21 at 08:41

4 Answers4

0

j++ will increment the value of j but return the original value of j before being incremented.

 int i =0;
 int j = 0;
 i = 1;
 j = i++;
 (i is 2, j is 1)

Source

HiEd
  • 160
  • 3
0

The postfix increment conceptually copies the operand in memory, increments the original operand, and finally yields the value of the copy.

The below snippet is fairly straightforward. The j has been incremented after printing the content of the vector.

vector<int> v(10);
for(int i = 0; i < v.size(); i++){
    v[i] = i;
}
int j = 0;

for(int i = 0; i < 1; i++){ // first loop
    while(v[j] != 5){
        cout << v[j]; //prints 0 1 2 3 4
        j++;
    }
}

Now, let's take the look at the 2nd loop.

vector<int> v(10);
for(int i = 0; i < v.size(); i++){
    v[i] = i;
}
int j = 0;
for(int i = 0; i < 1; i++){ // second loop
    // values of j are 0 1 2 3 4
    while(v[j++] != 5){
        // values of j are 1 2 3 4 5 because it has been incremented
        cout << v[j]; //prints 1 2 3 4 5
    }
}
Shravan40
  • 8,922
  • 6
  • 28
  • 48
0

As it is said above, j++ will increment the j so when j is 3, v[j++] is 4 so condition in while is not broken. But because of j++, when cout is executed j is 4 and v[4] is 5 so 5 gets printed

LazyLogik
  • 1
  • 2
0

You forgot to reinitialize j after a the first loop. If you want to print from 1 to 5 in the second loop, then you should reinitialize j as 0 before the second loop runs. Try:

vector<int> v(10);
    for (int i = 0; i < v.size(); i++) {
        v[i] = i;
    }
    int j = 0;
    for (int i = 0; i < 1; i++) { // first loop
        while (v[j] != 5) {
            cout << v[j]; //prints 0 1 2 3 4
            j++;
        }
    }
    j = 0;
    for (int i = 0; i < 1; i++) { //second loop
        while (v[j++] != 5) {
            cout << v[j]; //prints 1 2 3 4 5
        }
    }

using a single for loop will help understand the code better since both conditions are same for the loop.

    vector<int> v(10);
    for (int i = 0; i < v.size(); i++) {
        v[i] = i;
    }
    int j = 0;
    for (int i = 0; i < 1; i++) { // first loop
        while (v[j] != 5) {
            cout << v[j]; //prints 0 1 2 3 4
            j++;
        }
// joined the end of the first loop and the start of the second loop
        j = 0;
        while (v[j++] != 5) {
            cout << v[j]; //prints 1 2 3 4 5
        }
    }

Hope it will help in answering your question