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
}
}