Loops program error

Hi, i've done a simple program that has 2 variables (x and y) and displays their multiplication and keep adding one to x. When x = 5, it stops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;
int main()
{
    int x = 0;
    int y = 9;
    
    do
    {
        cout << "x * y = " << (x * y) << endl;
        x++;
        cin.get();
    }
while ( x == 6);
      {
          system("PAUSE");
          cin.get();
          }
}


The problem is that it only displays x * y = 0, instead of all the multiplications, until x = 5. What am i doing wrong? Wasnt that supposed to be correct? Please help ...
Your program is instructed to do lines 11-13 while x is equal to 6.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
    int x = 0;
    int y = 9;
    
    while (x<6)
    {
        cout << "x * y = " << (x * y) << endl;
        x++;
        cin.get();
    }
    system("PAUSE");
    cin.get();

    return 0;
}


should do the trick for you.

the do-while loop in your example continues for as long as x==6 which means x equals six. The comparision expression gets evaluated after the loop is passed for the first time, on which point x==1;

this means that the loop does not continue, since the result of x==6 is false.

x<6 results in true for every value below 6. The loop continues as long as the expression returns true, so at the moment x becomes 6 it ends.

Hope this helps.

Regards, Ronnie van Aarle.
Don't use a "do while loop" while a while loop will do
confused?
Last edited on
hmm, understood... I thought that , in this case, the program would execute whats within do, and when x = 6 it would stop. Exactly the opposite seymore said...:D Thank you all, im new to this
The original do-while loop was fine. You could have just changed the condition to ( x != 6 ) or, even better, ( x < 6 ).

I personally prefer while over do-while, but it's not my code... :P
Last edited on
yes, its easier...
Topic archived. No new replies allowed.