do {} while question (User==gulible)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std; // So the program can see cout and endl

int main()
{
    int x=0, j=1;
    do {
    cout<<"Enter any number other than 5: ";
    cin>>x;
    cin.ignore();
    if (x==5) {
        cout<<"You were not supposed to enter 5";
    }
    else if (j==10) {
        cout<<"You are more patient than I thought...you win.";
    }
        j++;
    } while(x!=5&&j<=10);

  cin.get();
}


Hi, I am a little confused by the inconsistency of what the code says and what the program does. The underlined code tells the program to continue to run as long as j is LESS than or equal 10.. but it continues running until 10 iterations of the program have passed. Can anyone explain this? Thank you
Where is the inconsistency? It does what it should
http://www.cplusplus.com/forum/articles/12974/ :
★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.
Well, when I read the code it says as long as j IS 10 or IS LESS THAN 10. Since the program is Always less then 10 until the 10th iteration I began writing that part of the code as: j==10 and it wasn't until I cheated and looked at how they wrote it that I found out it's supposed to be less than or equal to 10. I wrote "==" because I want the program to stop on the 10th iteration not on less than 10 iterations, which is what I thought it would do but it doesn't... it works fine.
You seem to think that when the while condition is true, the loop stops, but that is not the case.
It's the condition for which the loop continues.
while ( j <= 10 ) means "if j is less than ( or equal to ) 10 continue with the loop, if it's greater stop"
while ( j == 10 ) means "if j is equal to 10 continue with the loop, if it's anything else stop"
There's no inconsistency
That's why it's called while loop and not until loop. While the condition is true, the loop continues.
Thank you very much for all the in depth explanations. I completely understand my error now.
Topic archived. No new replies allowed.